一. 具名插槽 --> 父传子
语法:
父组件
<template v-slot:子组件slot内插槽名>
<h3>具名插槽</h3>
</template>//简写
<template #子组件slot内插槽名>
<h3>具名插槽简写</h3>
</template>
子组件
<slot name="自定义插槽名"> <slot>
作用:1.当子组件内有多处不确定的结构时使用
介绍: 具名插槽则是当子组件需要显示不同的效果时使用具名插槽,通过 name 属性给插槽命名 父组件向子组件的具名插槽提供内容的时候用 v-slot:name的插槽名 的形式提供内容
说明: 子组件中给插槽添加名字 父组件用v-slot指令传递具名插槽
应用场景: 当需要在子组件内不同的位置插入不同的内容时
一. 作用域插槽 -- 带数据插槽 --> 子传父
语法:
子组件
// 传字符串
<slot name="插槽名" yes="字符串" no="字符串" money="字符串"></slot>
//传Vue数据
<slot name="插槽名" :yes="data内变量名" :no="data内变量名" money="data内变量名"></slot>
父组件
<template #子组件slot内插槽名="obj">
<button>{{ obj.yes }}</button>
<button>{{ obj.no }}</button>
<button>{{ obj.money }}</button>
</template>//解构赋值接收
<template #子组件slot内插槽名="{ yes, no, money }">
<button>{{ yes }}</button>
<button>{{ no }}</button>
<button>{{ money }}</button>
</template>