vue3中插槽

具名插槽

vue2中会有这样用法:

//父组件
<template>
  <child-component>
    <template slot="empty">
      这是 empty 插槽的内容
    </template>
  </child-component>
</template>

slot=“empty” 这个写法是 Vue 2 中的具名插槽语法,用户可能在某个父组件中通过这个语法向子组件的特定插槽传递内容

而在 Vue 3 中,这种写法被废弃了

在 Vue 3 中,slot=“empty” 的用法已经被替换为新的语法。Vue 3 引入了一种更统一的插槽语法,移除了 Vue 2 中的具名插槽语法 (slot=“name”),改为使用 v-slot 指令。

vue3的用法

<template>
  <child-component>
    <template v-slot:empty >
      这是 empty 插槽的内容
    </template>
  </child-component>
</template>

Vue 3 还支持简写形式,直接使用 #

<template>
  <child-component>
    <template #empty>
      这是 empty 插槽的内容
    </template>
  </child-component>
</template>

总结

  • Vue 2 使用 slot=“empty”
  • Vue 3 使用 v-slot:empty 或简写 #empty

插槽作用域

在Vue3中,​slot-scope​​ 已被废弃,取而代之的是新的 ​​v-slot​​ 语法。虽然 ​​slot-scope​​ 在 Vue2 中用于定义作用域插槽,但在 Vue3 中,推荐使用 ​​v-slot​​ 进行插槽的定义和使用。注意:v-slot只能在template或者vue组件上使用,比如div上使用,会报错

// vue2语法
<div class="custom-tree-node" slot-scope="{ node, data, store }">
</div>

//vue3语法
<template class="custom-tree-node" v-slot="{ node, data, store }">
</template>
//或者针对具名插槽
<template class="custom-tree-node" v-slot:header="{ node, data, store }">
</template>
//也可以使用简写
<template class="custom-tree-node" #header="{ node, data, store }">
</template>
Vue 3中,插槽的使用方式与Vue 2有所不同。Vue 3中引入了新的组件声明方式,即使用`<script setup>`语法来定义组件。下面是Vue 3插槽的使用方法: 1. 默认插槽: 在父组件中,可以使用`<slot>`标签来定义默认插槽。子组件中的内容将会被插入到父组件中的`<slot>`标签所在的位置。例如: ```vue <!-- ParentComponent.vue --> <template> <div> <slot></slot> </div> </template> <!-- ChildComponent.vue --> <template> <div> <h1>Child Component</h1> <p>This is the content of the child component.</p> </div> </template> ``` 在父组件中使用子组件时,可以将子组件的内容放在父组件的标签内: ```vue <!-- App.vue --> <template> <div> <parent-component> <h2>Parent Component</h2> <p>This is the content of the parent component.</p> </parent-component> </div> </template> ``` 运行后,父组件中的`<slot>`标签将会被子组件的内容替换。 2. 具名插槽: 在父组件中,可以使用`<slot>`标签的`name`属性来定义具名插槽。子组件中的内容可以通过`<template v-slot:插槽名>`或`<template #插槽名>`来指定插入到具名插槽中。例如: ```vue <!-- ParentComponent.vue --> <template> <div> <slot name="header"></slot> <slot></slot> </div> </template> <!-- ChildComponent.vue --> <template> <div> <template v-slot:header> <h1>Header</h1> </template> <h2>Child Component</h2> <p>This is the content of the child component.</p> </div> </template> ``` 在父组件中使用子组件时,可以使用`<template v-slot:插槽名>`或`<template #插槽名>`来指定具名插槽的内容: ```vue <!-- App.vue --> <template> <div> <parent-component> <template v-slot:header> <h2>Parent Component Header</h2> </template> <h3>Parent Component</h3> <p>This is the content of the parent component.</p> </parent-component> </div> </template> ``` 运行后,父组件中的`<slot name="header">`标签将会被子组件的具名插槽内容替换。 3. 作用域插槽: 在Vue 3中,作用域插槽被称为"带有参数的插槽"。父组件可以通过在`<slot>`标签上使用`v-slot:参数名`或`#参数名`来接收子组件传递的数据。例如: ```vue <!-- ParentComponent.vue --> <template> <div> <slot name="header" v-bind:user="user"></slot> </div> </template> <!-- ChildComponent.vue --> <template> <div> <template v-slot:header="slotProps"> <h1>{{ slotProps.user.name }}</h1> </template> <h2>Child Component</h2> <p>This is the content of the child component.</p> </div> </template> <script> export default { data() { return { user: { name: &#39;John&#39;, age: 25 } }; } }; </script> ``` 在父组件中使用子组件时,可以通过`v-slot:参数名`或`#参数名`来接收子组件传递的数据: ```vue <!-- App.vue --> <template> <div> <parent-component> <template v-slot:header="slotProps"> <h2>{{ slotProps.user.name }} - {{ slotProps.user.age }}</h2> </template> <h3>Parent Component</h3> <p>This is the content of the parent component.</p> </parent-component> </div> </template> ``` 运行后,父组件中的`<slot name="header" v-bind:user="user">`标签将会被子组件的作用域插槽内容替换,并且可以访问子组件传递的数据。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

南朝听月

你的打赏是我持续贡献的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值