Vue组件插槽

文章目录

  • 插槽内容
  • 后备内容
  • 具名插槽
  • 作用域插槽


一、插槽内容

<slot>标签可以看成是提供给父组件使用的占位符

<script type="text/x-template" id="my-radio">
    <label>
         <input type="radio":name="name":value="value" >
         <slot></slot>
    </label>
</script>
Vue.component('my-radio',{
    template:'#my-radio',
    props:['name,','value']
})

 

二、后备内容

在组件prop中,可以通过default选项设置该属性的默认值,在插槽中同样可以设置默认内容,它只会在插槽没有提供内容时被渲染。

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

三、具名插槽

<slot>标签有一个特殊的属性name,当模版中需要使用多个插槽时,可以通过该属性进行分区。如果没有name属性,则等价于name="default"。

<script type="text/x-template" id="book">
    <div class="card">
        <div class="card-header">
            <div class="card-title">
                 <slot name="title"></slot>
            </div>
            <div class="card-btns">
                 <slot name="btns"></slot>
            </div>
        </div>
        <div class="card-body">
            <slot name="body"></slot>
        </div>
    </div>
</script>

四、作用域插槽

子组件中如果存在数据,那么该数据只能在子组件模版中进行使用,即在父组件中使用该子组件是访问不到子组件中的数据的

<span>
    <slot>{{user.no}}</slot>
</span>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值