slot插槽

用于向子组件传递结构

1.默认插槽

//父组件中
<zizujian>
  要传递的内容
</zizujian>
//子组件中
<slot />

2.具名插槽

//父组件中
<zizujian>
  <template v-slot:header>
    <h1>我是header</h1>
  </template>
  
  <template v-slot:footer>
    <h1>我是footer</h1>
  </template>
  
  <div></div>
</zizujian>
//子组件中
<slot name="header"/>
<slot name="footer"/>
<slot />

在子组件标签中,任何没有v-slot的内容都被认为是默认插槽的内容

3.作用域插槽

用于父组件访问子组件的数据

//子组件中
<template>
  <div>
    <slot v-bind:childData="childUser"></slot>
  </div>
</template>

<script>
export default {
  data() {
     return {
        childUser: { Name:"Tom", Age:23 }
    }
}
</script>
//父组件中
<zizujian>
  <template v-slot:default="slotProps">
    {{ slotProps.childData.Name}}
    {{ slotProps.childData.Age}}
  </template>
</zizujian>

如果被提供的内容只有默认插槽,可以直接写在子组件标签上

//父组件中
<zizujian v-slot:default="slotProps">
  {{ slotProps.childData.Name}}
  {{ slotProps.childData.Age}}
</zizujian>

默认插槽也可以不写default

//父组件中
<zizujian v-slot="slotProps">
  {{ slotProps.childData.Name}}
  {{ slotProps.childData.Age}}
</zizujian>

4.解构插槽

//子组件中
<template>
  <div>
    <slot v-bind:childData="childUser"></slot>
  </div>
</template>

<script>
export default {
  data() {
     return {
        childUser: { Name:"Tom", Age:23 }
    }
}
</script>
//父组件中
<zizujian v-slot="{childData}">
  {{ childData.Name}}
  {{ childData.Age}}
</zizujian>

将 childData重命名为 person

//父组件中
<zizujian v-slot="{childData:person}">
  {{ person.Name}}
  {{ person.Age}}
</zizujian>

也可以定义默认内容,用于插槽 prop 是 undefined 的情形

//父组件中
<zizujian v-slot="{childData= { Name: 'Guest' }}">
  {{ childData.Name }}
</zizujian>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值