美文网首页
vue中slot(插槽)

vue中slot(插槽)

作者: zjh111 | 来源:发表于2019-02-12 09:31 被阅读0次

未命名插槽

slot未命名时,每个<slot>标签内都会生成父组件中提供的内容。
父组件app.vue

<template>
 <div id='app'>
        <slottest>
            <h1>Here might be a page title</h1>
                  
            <p>A paragraph for the main content.</p>
            <p>And another one.</p>
                  
            <p>Here's some contact info</p>
        </slottest>
    </div>
</template>
<script>
import slottest from './components/slottest'
export default {
  data(){
    return {
      
    }
  },
  components:{
    slottest
  }
}
</script>

子组件slottest.vue

<template>
  <div>
    <slot></slot>
<slot></slot>
  </div> 
</template> 
 
<script>
export default { 
  data(){
    return {
      
    }
  }
}
</script>

<style>

</style>

渲染HTML如下

image.png
如果子组件中没有包含任何一个 <slot> 元素,则任何传入它的内容都会被抛弃。

命名插槽

父组件app.vue

<template>
    <div id='app'>
            <button-counter></button-counter>
        <slottest>
            <h1 slot="header">Here might be a page title</h1>
                  
            <p>A paragraph for the main content.</p>
            <p>And another one.</p>
                  
            <p slot="footer">Here's some contact info</p>
        </slottest>
    </div>
</template>
<script>
import slottest from './components/slottest'
export default {
  data(){
    return {
      
    }
  },
  components:{
    slottest
  }
}
</script>

子组件slottest.vue

<template>
  <div>
    <header>
    <slot name="header"></slot>
  </header>
  <main>
    <slot></slot>
  </main>
  <footer>
    <slot name="footer"></slot>
  </footer>
  </div> 
</template> 
 
<script>
export default { 
  data(){
    return {
      
    }
  }
}
</script>

<style>

</style>

渲染完后html结构


image.png

默认内容

<button type="submit">
  <slot>Submit</slot>
</button>

如果父组件为这个插槽提供了内容,则默认的内容会被替换掉。

作用域插槽 | 带数据的插槽

作用域插槽要求,在slot上面绑定数据。

<slot name="up" :data="data"></slot>
 export default {
    data: function(){
      return {
        data: ['zhangsan','lisi','wanwu','zhaoliu','tianqi','xiaoba']
      }
    },
}

父组件

<template>
  <div class="father">
    <h3>这里是父组件</h3>
    <!--第一次使用:用flex展示数据-->
    <child>
      <template slot-scope="user">
        <div class="tmpl">
          <span v-for="item in user.data">{{item}}</span>
        </div>
      </template>

    </child>

    <!--第二次使用:用列表展示数据-->
    <child>
      <template slot-scope="user">
        <ul>
          <li v-for="item in user.data">{{item}}</li>
        </ul>
      </template>

    </child>

    <!--第三次使用:直接显示数据-->
    <child>
      <template slot-scope="user">
       {{user.data}}
      </template>

    </child>

    <!--第四次使用:不使用其提供的数据, 作用域插槽退变成匿名插槽-->
    <child>
      我就是模板
    </child>
  </div>
</template>

子组件

<template>
  <div class="child">

    <h3>这里是子组件</h3>
    // 作用域插槽
    <slot  :data="data"></slot>
  </div>
</template>

 export default {
    data: function(){
      return {
        data: ['zhangsan','lisi','wanwu','zhaoliu','tianqi','xiaoba']
      }
    }
}
image.png

相关文章

  • Vue之深入理解插槽—slot, slot-scope, v-s

    Vue 2.6.0 以前Vue 2.6.0 以后具名插槽 slot具名插槽 v-slot作用域插槽 slot-sc...

  • vue中的slot(插槽)

    vue中的插槽————slot 什么是插槽? 插槽(Slot)是Vue提出来的一个概念,正如名字一样,插槽用于决定...

  • vue插槽

    vue插槽slot的理解与使用 vue slot插槽的使用介绍及总结

  • 18、Vue3 作用域插槽

    作用域插槽:让插槽内容能够访问子组件中,vue2中作用域插槽使用slot-scope,vue3中使用v-slot ...

  • vue 插槽的使用

    vue 插槽手册 深入理解vue中的slot与slot-scope 插槽的使用其实是很简单首先要明白插槽是使用在子...

  • Vue中Slot的渲染过程

    Vue在通过compiler解析模版中的slot, slot是组件中的插槽,通过解析slot,把slot的name...

  • 2020-07-23 一次性讲明白vue插槽slot

    vue插槽slot 一、前言 vue官方文档中在"组件基础"内容中提到组件可以通过插槽分发内容,那插槽是怎么使用的...

  • vue插槽slot

    vue插槽slot 一、前言 vue官方文档中在"组件基础"内容中提到组件可以通过插槽分发内容,那插槽是怎么使用的...

  • Vue3.0 组件的核心概念_插槽

    Vue 在 2.6 版本中,对插槽使用 v-slot 新语法,取代了旧语法的 slot 和 slot-scope,...

  • vue3-slot-消息框-模态框

    1.前言 1.使用vue3 的slot插槽时,大部分和vue2-slot插槽[https://www.jiansh...

网友评论

      本文标题:vue中slot(插槽)

      本文链接:https://www.haomeiwen.com/subject/ubrheqtx.html