溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Vue中匿名、具名和作用域插槽有什么用

發(fā)布時間:2021-08-07 10:07:11 來源:億速云 閱讀:132 作者:小新 欄目:web開發(fā)

這篇文章給大家分享的是有關(guān)Vue中匿名、具名和作用域插槽有什么用的內(nèi)容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。

Vue 中的插槽在開發(fā)組件的過程中其實是非常重要并且好用的。Vue 的插槽也沒有說很難使用,這篇文章簡明扼要的介紹了三種插槽的用法。

匿名插槽

子組件定義 slot 插槽,但并未具名,因此也可以說是默認(rèn)插槽。只要在父元素中插入的內(nèi)容,默認(rèn)加入到這個插槽中去。 

<template>
 <div>
 hello <slot>陌生人</slot>
 </div>
</template>

這里定義了一個默認(rèn)插槽,只要往里頭丟東西,就會被加入到這個插槽里面

slot 元素里面可以加入一系列后備內(nèi)容,一旦父元素沒有插入任何信息,那么就會渲染后備內(nèi)容。

<my-comp>
 oli
</my-comp>

如在父組件中使用這個子組件,并插入 oli 字符串,效果如下:

Vue中匿名、具名和作用域插槽有什么用

具名插槽

具名插槽可以出現(xiàn)在不同的地方,不限制出現(xiàn)的次數(shù)。只要匹配了 name 那么這些內(nèi)容就會被插入到這個 name 的插槽中去。

<template>
 <div>
 <slot name="nav"></slot>
 <br/>
 <slot name="content"></slot>
 <br/>
 <slot name="footer"></slot>
 </div>
</template>

比如上述代碼定義了三個具名插槽。在父組件中即可使用 slot 屬性插入到對應(yīng)的插槽中:

<template>
 <div>
 <my-comp>
 <template slot="nav">navigator</template>
 <template slot="footer">footer</template>
 <template slot="content">content</template>
 </my-comp>
 </div>
</template>

另外,順序并不重要,content 在 footer 下方但依然能夠按照 slot 定義的順序渲染:

Vue中匿名、具名和作用域插槽有什么用

作用域插槽

通常情況下普通的插槽是父組件使用插槽過程中傳入東西決定了插槽的內(nèi)容。但有時我們需要獲取到子組件提供的一些數(shù)據(jù),那么作用域插槽就排上用場了。 ?

在子組件中創(chuàng)建 slot 并通過 v-bind 綁定數(shù)據(jù) prop 的形式傳入數(shù)據(jù):

<slot :data="data"></slot>

在組件 data 中創(chuàng)建數(shù)據(jù):

export default {
 name: 'MyComp',
 data () {
 return {
 data: { // 內(nèi)部狀態(tài)
 username: 'oli'
 }
 }
 }
}

這樣就可以在插槽中訪問到子元素的數(shù)據(jù)了:

<template v-slot:default="user">{{user.data.username}}</template>

也可以不書寫 default 關(guān)鍵字,默認(rèn)就是假定對應(yīng)默認(rèn)插槽

<template v-slot="user">{{user.data.username}}</template>

使用 v-slot 綁定一個命名空間 user,這樣就可以通過 user 對象引用到子組件中傳入的數(shù)據(jù)了

與具名插槽配合時,需要明確書寫對應(yīng)的命名空間:

<template #:one="user">{{user.data.username}}</template>
<template #:another="user">{{user.data.username}}</template>
# 代表 v-slot 的縮寫,縮寫在有參數(shù)的情況下才會生效

動態(tài)插槽名

另外,2.6 版本的 vue 還加入了動態(tài)插槽名的功能,用來動態(tài)的定義插槽名稱:

<template #:[dynamicSlotName]></template>

https://cn.vuejs.org/v2/guide...

PS:Vue作用域插槽使用實例詳解

這次給大家?guī)鞻ue作用域插槽使用詳解,Vue作用域插槽使用的注意事項有哪些,下面就是實戰(zhàn)案例,一起來看一下。

舉個例子,比如我寫了一個可以實現(xiàn)條紋相間的列表組件,發(fā)布后,使用者可以自定義每一行的內(nèi)容或樣式(普通的slot就可以完成這個工作)。而作用域插槽的關(guān)鍵之處就在于,父組件能接收來自子組件的slot傳遞過來的參數(shù),具體看案例和注釋。

<!DOCTYPE html>
 <htmllang="en">
 <head>
  <metacharset="UTF-8">
  <title>Vue作用域插槽</title>
  <scriptsrc="https://cdn.bootcss.com/vue/2.3.4/vue.js"></script>
 </head>
 <body>
  <pid="app2">
   <my-stripe-list:items="users"odd-bgcolor="#D3DCE6"even-bgcolor="#E5E9F2">
    <!-- props對象接收來自子組件slot的$index參數(shù) -->
    <templateslot="cont"scope="props">
     <span>{{users[props.$index].id}}</span>
     <span>{{users[props.$index].name}}</span>
     <span>{{users[props.$index].age}}</span>
     <!-- 這里可以自定[編輯][刪除]按鈕的鏈接和樣式 -->
     <a:href="'#edit/id/'+users[props.$index].id"rel="external nofollow">編輯</a>
     <a:href="'#del/id/'+users[props.$index].id"rel="external nofollow">刪除</a>
    </template>
   </my-stripe-list>
  </p>
  <script>
   Vue.component('my-stripe-list', {
    /*slot的$index可以傳遞到父組件中*/
    template: `
     <p>
      <pv-for="(item, index) in items":>
       <slotname="cont":$index="index"></slot>
      </p>
     </p>
    `,
    props: {
     items: Array,
     oddBgcolor: String,
     evenBgcolor: String
    }
   });
   new Vue({
    el: '#app2',
    data: {
     users: [
      {id: 1, name: '張三', age: 20},
      {id: 2, name: '李四', age: 22},
      {id: 3, name: '王五', age: 27},
      {id: 4, name: '張龍', age: 27},
      {id: 5, name: '趙虎', age: 27}
     ]
    }
   });
  </script>
 </body>
</html>

感謝各位的閱讀!關(guān)于“Vue中匿名、具名和作用域插槽有什么用”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學(xué)到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

vue
AI