深入组件:作用域插槽

F12

绑定在 slot 元素上的 attribute 被称为插槽 prop。
现在在父级作用域中,我们可以使用带值的 v-slot 来定义我们提供的插槽 prop 的名字。


We can add named slots to our component and access data from the child in the parent with slot props.
从 父组件 访问 子组件slot提供的prop数据,渲染后交给子组件slot占位符。
https://thewebdev.info/2020/07/26/vue-3%E2%80%8A-%E2%80%8Anamed-slots-and-slot-props/

注意默认插槽的缩写语法不能和具名插槽混用,因为它会导致作用域不明确。使用 具名slot的完整写法。
check: {{msg}}
1 basic
2 我们可能会想把 item 替换为 slot,以便在父组件上 接收 子组件binding的 数据 并自定义格式,最后传给 子组件slot。
我们选择将包含所有插槽 prop 的对象命名为 slotProps,但你也可以使用任意你喜欢的名字。
3 测试 修改名字
4 简写形式 v-slot="slotProps4" {{ slotProps4.item3 }}
5 多个 slot 时,要指定名字: html中 template 使用 v-lot:name2, 组件中使用 slot name=name2