Vue 插槽:slot、slot-scope 与指令 v-slot 应用讲解

不具名插槽

<body>
   <div id="app">
      <Test>
         <div>slot插槽占位内容</div>
      </Test>
   </div>
   <template id="test">
      <div>
         <slot></slot>//定义插槽
         <h3>这里是test组件</h3>
      </div>
   </template>
   
</body>

<script>
   Vue.component('Test',{
      template:"#test"
   });

   new Vue({
      el:"#app",
   })
</script>

具名插槽

<body>
   <div id="app">
      <Test>
         <div slot="header">这里是头部</div>//具名插槽使用
         <div slot="footer">这里是尾部</div>
      </Test>
   </div>
   <template id="test">
      <div>
         <slot name="header"></slot>//具名插槽
         <h3>这里是Test组件</h3>
         <slot name="footer"></slot>
      </div>

   </template>
</body>
<script>
   Vue.component(
      'Test',{
         template:"#test"
   });
   new Vue({
      el:"#app"
   })

</script>

v-slot

v-slot在组件中使用slot进行占位时,也是在slot标签内使用name 属性给slot插槽定义一个名字。但是在html内使用时就有些不同了。需要使用template模板标签,template标签内,使用v-slot指令绑定插槽名,标签内写入需要添加的内容。
<body>
   <div id="app">
      <Test>
         <template v-slot:header>//v-slot指令使用插槽
            <h2>slot头部内容</h2>
         </template>
         
         <p>直接插入组件的内容</p>
         
         <template v-slot:footer>
            <h2>slot尾部内容</h2>
         </template>
      </Test>
   </div>
   
   <template id ='test'>
      <div class="container">
         <header>
            <!-- 我们希望把页头放这里 -->
            <slot name = "header"></slot>//具名插槽
         </header>
         <section>
            主体内容部分
         </section>
         <footer>
            <!-- 我们希望把页脚放这里 -->
            <slot name = 'footer'></slot>
         </footer>
      </div>
   </template>
   
</body>

<script>
   Vue.component('Test',{
      template:"#test"
   });
   new Vue({
      el:"#app"
   })
</script>

slot-scope 与 v-slot

slot-scope

在组件模板中书写所需slot插槽,并将当前组件的数据通过v-bind绑定在slot标签上。

在组件使用时,通过slot-scope=“scope”,接收组件中slot标签上绑定的数据。

通过scope.xxx就可以使用绑定数据了

<body>
   <div id="app">
      <Test>
         <div slot="default" slot-scope="scope">//作用域插槽的用法(slot-scope)
            {{ scope.msg }}
         </div>
         
      </Test>
   </div>

   <template id="test">
      <div>
         <slot name="default" :msg="msg"> </slot>
         <p>这里是test组件</p>
      </div>
   </template>
</body>
----------------------------------------------------
<script>
   new Vue({
      el:"#app",
      components:{
         'Test':{
            template:"#test",
            data(){
               return {
                  msg:"你好"
               }
            },
         }
      }
   })
</script>

v-slot

<body>
   <div id="app">
      <Test>
         <template v-slot:header="scope">//v-slot定义作用域插槽
            <div>
                  <h3>slot</h3>
                  <p> {{scope.msg}} </p>
            </div>
         </template>
      </Test>
   </div>
   
   <template id="test">
      <div>
         <slot name="header":msg="msg"></slot>
         <p>这里是test组件</p>
      </div>
   </template>
   
</body>
<script>
   Vue.component('Test',{
      template:"#test",
      data(){
         return {
            msg:'这里是头部'
         }
      }
   });

   new Vue({

   }).$mount("#app")
</script>


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue中,插槽slot)是一种用于组件之间内容分发的机制。它允许父组件向子组件传递内容,使得子组件可以根据需要展示不同的内容。插槽可以用于传递任意类型的内容,包括HTML、文本和其他组件。 Vue中的插槽有三种使用方式:slotslot-scope和v-slot。 1. slotslot标签是最基本的插槽使用方式。父组件可以在子组件的slot标签中放置内容,这些内容将会替换子组件中具名插槽的位置。例如: ```html <!-- 父组件 --> <template> <child-component> <template v-slot:header> <h1>这是标题</h1> </template> </child-component> </template> <!-- 子组件 --> <template> <div> <slot name="header"></slot> <!-- 其他子组件内容 --> </div> </template> ``` 上述代码中,父组件在子组件的插槽中放置了一个标题,子组件通过`<slot name="header"></slot>`来展示这个插槽的内容。 2. slot-scopeslot-scope指令用于在插槽中接收父组件传递的数据。通过这种方式,子组件可以访问父组件的数据,并在插槽中进行处理。例如: ```html <!-- 父组件 --> <template> <child-component> <template v-slot:default="slotProps"> <h1>{{ slotProps.title }}</h1> <p>{{ slotProps.content }}</p> </template> </child-component> </template> <!-- 子组件 --> <template> <div> <slot :title="title" :content="content"></slot> <!-- 其他子组件内容 --> </div> </template> <script> export default { data() { return { title: '这是标题', content: '这是内容' }; } }; </script> ``` 在上述代码中,父组件通过`v-slot:default="slotProps"`将数据传递给子组件的插槽,并在插槽中使用`slotProps`来访问这些数据。 3. v-slot:v-slot指令Vue2.6版本及以上引入的新特性,用于简化插槽的语法。它可以直接在子组件上使用,而不需要使用template标签。例如: ```html <!-- 父组件 --> <template> <child-component> <template #header> <h1>这是标题</h1> </template> </child-component> </template> <!-- 子组件 --> <template> <div> <slot name="header"></slot> <!-- 其他子组件内容 --> </div> </template> ``` 在上述代码中,父组件使用`#header`来定义插槽,并在子组件中使用`<slot name="header"></slot>`来展示插槽的内容。 总结一下,slot用于定义插槽的位置,slot-scope用于接收父组件传递的数据,并在插槽中进行处理,v-slot是对插槽语法的简化。这些插槽的使用方式可以根据具体需求选择适合的方式。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值