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

何時(shí)何地使用 Vue 的作用域插槽

開(kāi)發(fā)
盡管Vue 作用域插槽是一個(gè)非常簡(jiǎn)單的概念-讓插槽內(nèi)容可以訪問(wèn)子組件數(shù)據(jù),這在設(shè)計(jì)出色的組件方面很有用處。通過(guò)將數(shù)據(jù)保留在一個(gè)位置并將其綁定到其他位置,管理不同狀態(tài)變得更加清晰。

[[392987]]

Vue插槽是一種將內(nèi)容從父組件注入子組件的絕佳方法。

下面是一個(gè)基本的示例,如果我們不提供父級(jí)的任何slot位的內(nèi)容,剛父級(jí)<slot>中的內(nèi)容就會(huì)作為后備內(nèi)容。

  1. // ChildComponent.vue 
  2. <template> 
  3.   <div> 
  4.      <slot> Fallback Content </slot> 
  5.   </div> 
  6. </template> 

然后在我們的父組件中:

  1. // ParentComponent.vue 
  2. <template> 
  3.    <child-component> 
  4.       Override fallback content 
  5.    </child-component> 
  6. </template> 

編譯后,我們的DOM將如下所示。

  1. <div> Override fallback content </div> 

我們還可以將來(lái)自父級(jí)作用域的任何數(shù)據(jù)包在在 slot 內(nèi)容中。因此,如果我們的組件有一個(gè)名為name的數(shù)據(jù)字段,我們可以像這樣輕松地添加它。

  1. <template> 
  2.    <child-component> 
  3.       {{ text }}  
  4.    </child-component> 
  5. </template> 
  6.  
  7. <script> 
  8. export default { 
  9.    data () { 
  10.      return { 
  11.        text: 'hello world'
  12.      } 
  13.    } 
  14. </script> 

為什么我們需要作用域插槽

我們來(lái)看另一個(gè)例子,假設(shè)我們有一個(gè)ArticleHeader組件,data 中包含了一些文章信息。

  1. // ArticleHeader.vue 
  2. <template> 
  3.   <div> 
  4.     <slot v-bind:info="info"> {{ info.title }} </slot> 
  5.   </div> 
  6. </template> 
  7.  
  8. <script> 
  9. export default { 
  10.   data() { 
  11.     return { 
  12.       info: { 
  13.         title: 'title'
  14.         description: 'description'
  15.       }, 
  16.     } 
  17.   }, 
  18. </script> 

我們細(xì)看一下 slot 內(nèi)容,后備內(nèi)容渲染了 info.title。

在不更改默認(rèn)后備內(nèi)容的情況下,我們可以像這樣輕松實(shí)現(xiàn)此組件。

  1. // ParentComponent.vue 
  2. <template> 
  3.   <div> 
  4.     <article-header /> 
  5.   </div> 
  6. </template> 

在瀏覽器中,會(huì)顯示 title。

雖然我們可以通過(guò)向槽中添加模板表達(dá)式來(lái)快速地更改槽中的內(nèi)容,但如果我們想從子組件中渲染info.description,會(huì)發(fā)生什么呢?

我們想像用下面的這種方式來(lái)做:

  1. // Doesn't work
  2. <template> 
  3.   <div> 
  4.     <article-header> 
  5.         {{ info.description }} 
  6.     </article-header> 
  7.   </div> 
  8. </template> 

但是,這樣運(yùn)行后會(huì)報(bào)錯(cuò) :TypeError: Cannot read property ‘description’ of undefined。

這是因?yàn)槲覀兊母附M件不知道這個(gè)info對(duì)象是什么。

那么我們?cè)撊绾谓鉀Q呢?

引入作用域插槽

簡(jiǎn)而言之,作用域內(nèi)的插槽允許我們父組件中的插槽內(nèi)容訪問(wèn)僅在子組件中找到的數(shù)據(jù)。 例如,我們可以使用作用域限定的插槽來(lái)授予父組件訪問(wèn)info的權(quán)限。

我們需要兩個(gè)步驟來(lái)做到這一點(diǎn):

  • 使用v-bind讓slot內(nèi)容可以使用info
  • 在父級(jí)作用域中使用v-slot訪問(wèn)slot屬性

首先,為了使info對(duì)父對(duì)象可用,我們可以將info對(duì)象綁定為插槽上的一個(gè)屬性。這些有界屬性稱為slot props。

  1. // ArticleHeader.vue 
  2. <template> 
  3.   <div> 
  4.     <slot v-bind:info="info"> {{ info.title }} </slot> 
  5.   </div> 
  6. </template> 

然后,在我們的父組件中,我們可以使用<template>和v-slot指令來(lái)訪問(wèn)所有的 slot props。

  1. // ParentComponent.vue  
  2. <template> 
  3.   <div> 
  4.     <child-component> 
  5.       <template v-slot="article"
  6.       </template> 
  7.     </child-component> 
  8.   </div> 
  9. </template> 

現(xiàn)在,我們所有的slot props,(在我們的示例中,僅是 info)將作為article對(duì)象的屬性提供,并且我們可以輕松地更改我們的slot以顯示description內(nèi)容。

  1. // ParentComponent.vue  
  2. <template> 
  3.   <div> 
  4.     <child-component> 
  5.       <template v-slot="article"
  6.         {{ article.info.description }} 
  7.       </template> 
  8.     </child-component> 
  9.   </div> 
  10. </template> 

最終的效果如下:

總結(jié)

盡管Vue 作用域插槽是一個(gè)非常簡(jiǎn)單的概念-讓插槽內(nèi)容可以訪問(wèn)子組件數(shù)據(jù),這在設(shè)計(jì)出色的組件方面很有用處。通過(guò)將數(shù)據(jù)保留在一個(gè)位置并將其綁定到其他位置,管理不同狀態(tài)變得更加清晰。

~完,我是刷碗智,我要去刷碗了,骨得白

作者:Ashish Lahoti  譯者:前端小智  來(lái)源:codingnconcept

原文:https://learnvue.co/2021/03/when-why-to-use-vue-scoped-slots/

 

責(zé)任編輯:武曉燕 來(lái)源: 大遷世界
相關(guān)推薦

2009-08-01 23:08:16

2020-05-25 17:03:47

Vue嵌套插槽開(kāi)發(fā)

2010-12-31 10:33:39

無(wú)線運(yùn)營(yíng)商光纖寬帶

2019-10-15 09:05:07

域插槽組件前端

2021-05-08 07:37:32

Vue 命名插槽

2021-12-29 07:51:21

Vue3 插件Vue應(yīng)用

2013-11-27 09:26:02

遠(yuǎn)程公司

2020-03-24 08:32:24

vue作用域前端

2022-07-15 08:45:07

slotVue3

2024-06-03 10:00:51

Vue 3語(yǔ)法插槽

2021-09-03 08:23:21

Vue 插槽子組件

2020-10-21 14:54:02

RustGolang開(kāi)發(fā)

2022-08-18 16:01:22

數(shù)據(jù)泄露網(wǎng)絡(luò)攻擊

2009-10-20 22:42:28

2021-07-12 10:23:24

物聯(lián)網(wǎng)傳感器洪水

2009-06-12 09:49:25

EJB事務(wù)屬性EJB事物

2010-09-29 15:02:23

DHCP作用域

2011-08-08 15:43:01

MySQL索引

2021-11-26 09:00:00

數(shù)據(jù)庫(kù)數(shù)據(jù)集工具

2021-04-12 07:34:03

Java集合框架
點(diǎn)贊
收藏

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