Vue插槽:(2.6.0以后版本弃用slot和slot-scope,改用v-slot)

关于Vue插槽的概念,大家可以从vue官网的api查看,我是看到网站的对于初接触 这个要概念的人来说不是很清楚,我来贴下原码,就比较直观了

贴下原码:

具名插槽:v-slot:header

Html:

<div id=’app’>

   <child>

       <template v-slot:header>

          <div>this is a header</div>

</template>

</child>

</div>

script部分:

Vue.component(‘child’,{

 Template:’<div><slot name=’header’></slot></div>

}

作用域插槽

Html:

<div id=’app’>

   <child>

       <template v-slot=’list’>

          <div>{{list.item}}</div>

</template>

</child>

</div>

script部分:

Vue.component(‘child’,{

data:function(){

 return{

      List:[1,2,3,4,5,6]

}

}

,

 Template:’<div><slot v-for=”item of list” :item=item></slot></div>

}

转载于:https://www.cnblogs.com/adah/p/10449787.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue中,slot)是一种用于组件之间内容分发的机制。它允许父组件向子组件传递内容,使得子组件可以根据需要展示不同的内容。可以用于传递任意类型的内容,包括HTML、文本和其他组件。 Vue中的有三种使用方式:slotslot-scopev-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-slotv-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、付费专栏及课程。

余额充值