Vue3 学习笔记—插槽使用大全

开发 前端
在 2.6.0中,vue 为具名插槽和作用于插槽引入了一个新的统一语法:v-slot。它取代了 slot 和 slot-scope 在新版中的应用。本篇文章主要介绍在 vue3 中插槽的使用。

[[442721]]

在 2.6.0中,vue 为具名插槽和作用于插槽引入了一个新的统一语法:v-slot。它取代了 slot 和 slot-scope 在新版中的应用。

本篇文章主要介绍在 vue3 中插槽的使用。

一、v-slot 介绍

v-slot 只能用在 template 或组件上使用,否则就会报错。

v-slot 也是其中一种指令。

使用示例:

  1. //父组件代码 
  2. <child-com> 
  3.  <template v-slot:nameSlot> 
  4.   插槽内容 
  5.  </template> 
  6. </child-com> 
  7.  
  8. //组件模板 
  9. <slot name="nameSlot"></slot> 

 v-slot 的语法,简化 slot、slot-scope 作用域插槽的功能,相比更加强大,代码效率更高。

二、匿名插槽

当组件中只有一个插槽的时候,可以不设置 slot 的 name 属性,v-slot 后可以不带参数,但是 v-slot 在没有设置 name 属性的插槽口也会带有隐含的 “default”。

匿名插槽使用:

  1. //组件调用 
  2. <child-com> 
  3.  <template v-slot> 
  4.   插槽内容 
  5.  </template> 
  6. </child-com> 
  7.  
  8. //组件模板 
  9. <slot ></slot> 

 虽然 v-slot 没有设置参数,但不能删除掉 ,否则插槽内容无法正常渲染。

三、具名插槽

一个组件中有多个插槽的时候,如果没有设置 v-slot 属性值,会默认把元素插到没有设置 name 属性值的 slot 组件中,为了把对应的元素放到指定的位置,就需要借助 v-slot 和 name 属性,把内容对应起来。

具名插槽使用:

  1. //父组件 
  2. <child-com> 
  3.  <template v-slot:header> 
  4.   头部 
  5.  </template> 
  6.  <template v-slot:body> 
  7.   内容 
  8.  </template> 
  9.  <template v-slot:footer> 
  10.   脚 
  11.  </template> 
  12. </child-com> 
  13.      
  14. //子组件   
  15. <div> 
  16.  <slot name="header"></slot> 
  17.  <slot name="body"></slot> 
  18.  <slot name="footer"></slot> 
  19. </div> 

具名插槽缩写

v-slot 与 v-bind、v-on 指令一样,也存在缩写。可以把 v-slot: 简写为 # 号。

如上述 v-slot:footer 可以简写为 #footer 。

上述的父组件代码可以简化为:

  1. <child-com> 
  2.  <template #header> 
  3.   头部 
  4.  </template> 
  5.  <template #body> 
  6.   内容 
  7.  </template> 
  8.  <template #footer> 
  9.   脚 
  10.  </template> 
  11. </child-com> 

注意:和其他指令一样,只有存在参数时,才可以简写,否则是无效的。

四、作用域插槽

有时让插槽内容能够访问子组件中才有的数据是很有用的。当一个组件被用来渲染一个项目数组时,这是一个常见的情况,我们希望能够自定义每个项目的渲染方式。

要使子组件上的属性在插槽内容上可用,需要给 slot 绑定一个属性。然后在 v-slot 处接收并定义提供插槽 props 名字。

使用示例:

  1. // 
  2. <child-com> 
  3.  <template v-slot:header="slotProps"
  4.   插槽内容--{{ slotProps.item }} 序号--{{ slotProps.index }} 
  5.  </template> 
  6. </child-com> 
  7.      
  8. //子组件代码 
  9. <template> 
  10.  <div v-for="(item, index) in arr" :key="index"
  11.   <slot :item="item" name="header" :index="index"></slot> 
  12.  </div> 
  13. </template> 
  14. <script setup> 
  15.  const arr = ['1111''2222''3333'
  16. </script> 

五、动态插槽名

v-slot 指令参数也可以是动态的,用来定义动态插槽名。

如:

  1. <child-com> 
  2.  <template v-slot:[dd()]> 
  3.   动态插槽名 
  4.  </template> 
  5. </child-com> 
  6.  
  7. <script setup> 
  8. const dd = () => { 
  9.   return 'hre' 

此处使用的是函数,也可以直接使用变量。

 

责任编辑:姜华 来源: 今日头条
相关推荐

2022-07-15 08:45:07

slotVue3

2021-12-01 08:11:44

Vue3 插件Vue应用

2021-11-30 08:19:43

Vue3 插件Vue应用

2021-11-16 08:50:29

Vue3 插件Vue应用

2023-11-28 09:03:59

Vue.jsJavaScript

2021-11-17 08:24:47

Vue3 插件Vue应用

2023-12-14 08:25:14

WatchVue.js监听数据

2021-12-08 09:09:33

Vue 3 Computed Vue2

2021-12-02 05:50:35

Vue3 插件Vue应用

2023-12-11 07:34:37

Computed计算属性Vue3

2023-11-29 08:49:31

Vue.jsData 函数

2021-05-08 07:37:32

Vue 命名插槽

2021-11-26 05:59:31

Vue3 插件Vue应用

2023-12-06 07:43:56

Vue如何定义事件

2020-09-19 21:15:26

Composition

2021-12-15 08:23:42

Vue3 插件Vue应用

2020-05-25 17:03:47

Vue嵌套插槽开发

2022-09-06 12:20:30

Vue3CVCRUD

2022-02-18 09:39:51

Vue3.0Vue2.0Script Set

2021-01-15 05:16:37

Vue3开源代码量
点赞
收藏

51CTO技术栈公众号