自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Vue3 學習筆記—插槽使用大全

開發(fā) 前端
在 2.6.0中,vue 為具名插槽和作用于插槽引入了一個新的統(tǒng)一語法:v-slot。它取代了 slot 和 slot-scope 在新版中的應用。本篇文章主要介紹在 vue3 中插槽的使用。

[[442721]]

在 2.6.0中,vue 為具名插槽和作用于插槽引入了一個新的統(tǒng)一語法:v-slot。它取代了 slot 和 slot-scope 在新版中的應用。

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

一、v-slot 介紹

v-slot 只能用在 template 或組件上使用,否則就會報錯。

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

使用示例:

  1. //父組件代碼 
  2. <child-com> 
  3.  <template v-slot:nameSlot> 
  4.   插槽內(nèi)容 
  5.  </template> 
  6. </child-com> 
  7.  
  8. //組件模板 
  9. <slot name="nameSlot"></slot> 

 v-slot 的語法,簡化 slot、slot-scope 作用域插槽的功能,相比更加強大,代碼效率更高。

二、匿名插槽

當組件中只有一個插槽的時候,可以不設(shè)置 slot 的 name 屬性,v-slot 后可以不帶參數(shù),但是 v-slot 在沒有設(shè)置 name 屬性的插槽口也會帶有隱含的 “default”。

匿名插槽使用:

  1. //組件調(diào)用 
  2. <child-com> 
  3.  <template v-slot> 
  4.   插槽內(nèi)容 
  5.  </template> 
  6. </child-com> 
  7.  
  8. //組件模板 
  9. <slot ></slot> 

 雖然 v-slot 沒有設(shè)置參數(shù),但不能刪除掉 ,否則插槽內(nèi)容無法正常渲染。

三、具名插槽

一個組件中有多個插槽的時候,如果沒有設(shè)置 v-slot 屬性值,會默認把元素插到?jīng)]有設(shè)置 name 屬性值的 slot 組件中,為了把對應的元素放到指定的位置,就需要借助 v-slot 和 name 屬性,把內(nèi)容對應起來。

具名插槽使用:

  1. //父組件 
  2. <child-com> 
  3.  <template v-slot:header> 
  4.   頭部 
  5.  </template> 
  6.  <template v-slot:body> 
  7.   內(nèi)容 
  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.   內(nèi)容 
  7.  </template> 
  8.  <template #footer> 
  9.   腳 
  10.  </template> 
  11. </child-com> 

注意:和其他指令一樣,只有存在參數(shù)時,才可以簡寫,否則是無效的。

四、作用域插槽

有時讓插槽內(nèi)容能夠訪問子組件中才有的數(shù)據(jù)是很有用的。當一個組件被用來渲染一個項目數(shù)組時,這是一個常見的情況,我們希望能夠自定義每個項目的渲染方式。

要使子組件上的屬性在插槽內(nèi)容上可用,需要給 slot 綁定一個屬性。然后在 v-slot 處接收并定義提供插槽 props 名字。

使用示例:

  1. // 
  2. <child-com> 
  3.  <template v-slot:header="slotProps"
  4.   插槽內(nèi)容--{{ 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> 

五、動態(tài)插槽名

v-slot 指令參數(shù)也可以是動態(tài)的,用來定義動態(tài)插槽名。

如:

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

此處使用的是函數(shù),也可以直接使用變量。

 

責任編輯:姜華 來源: 今日頭條
相關(guān)推薦

2022-07-15 08:45:07

slotVue3

2021-12-01 08:11:44

Vue3 插件Vue應用

2021-11-30 08:19:43

Vue3 插件Vue應用

2023-11-28 09:03:59

Vue.jsJavaScript

2021-11-16 08:50:29

Vue3 插件Vue應用

2023-12-14 08:25:14

WatchVue.js監(jiān)聽數(shù)據(jù)

2021-11-17 08:24:47

Vue3 插件Vue應用

2021-12-02 05:50:35

Vue3 插件Vue應用

2021-12-08 09:09:33

Vue 3 Computed Vue2

2023-12-11 07:34:37

Computed計算屬性Vue3

2023-11-29 08:49:31

Vue.jsData 函數(shù)

2021-11-26 05:59:31

Vue3 插件Vue應用

2023-12-06 07:43:56

Vue如何定義事件

2024-06-03 10:00:51

Vue 3語法插槽

2021-05-08 07:37:32

Vue 命名插槽

2024-11-06 10:16:22

2020-09-19 21:15:26

Composition

2022-09-06 12:20:30

Vue3CVCRUD

2020-05-25 17:03:47

Vue嵌套插槽開發(fā)

2021-12-15 08:23:42

Vue3 插件Vue應用
點贊
收藏

51CTO技術(shù)棧公眾號