slot 內(nèi)容分發(fā)(組件相互嵌套)

上一篇:非父子組件間通信

slot 內(nèi)容分發(fā):為了讓組件可以組合,我們需要一種方式來混合父組件的內(nèi)容與子組件自己的模板。

比如我們遇到組件需要相互嵌套這樣的情況:
App.vue 文件

<template>
    <div class="app">
        <Hello>
          <World></World>
          <Vue></Vue>
        </Hello>
    </div>
</template>

Hello.vue 文件

<template>
    <div class="hello">
        <h3>我是 hello 組件</h3>
    </div>
</template>

那這么直接寫是肯定只顯示 <Hello> 組件的內(nèi)容

所以就需要用到 slot 插口,否則該組件標簽中的內(nèi)容將會被丟棄

一、 單個插槽

只需要在 Hello.vue 文件內(nèi)寫入 <solt></slot> 標簽

<template>
    <div class="hello">
        <h3>我是 hello 組件</h3>
        <slot>此處可自定義內(nèi)容,如果該組件的標簽內(nèi)沒有內(nèi)容,則顯示此內(nèi)容</slot>
    </div>
</template>

注意:<solt></slot> 標簽內(nèi)是可以自定義內(nèi)容的,如果在調(diào)用的 <Hello></Hello> 標簽內(nèi)沒有寫入任何內(nèi)容,則顯示 <solt></slot> 標簽內(nèi)的自定義內(nèi)容,否則顯示在調(diào)用的 <Hello></Hello> 標簽內(nèi)寫入的內(nèi)容

二、多個插槽

假如我們想要改變 <Hello></Hello> 組件內(nèi) <World></World><Vue></Vue> 的顯示位置,就可以用到一個特殊的特性 name 來進一步配置如何分發(fā)內(nèi)容,多個插槽可以有不同的名字。具名插槽將匹配內(nèi)容片段中有對應 slot 特性的元素。仍然可以有一個匿名插槽,它是默認插槽,作為找不到匹配的內(nèi)容片段的備用插槽。如果沒有默認插槽,這些找不到匹配的內(nèi)容片段將被拋棄。

App.vue 文件

<template>
    <div class="app">
        <Hello>
          <World slot="world"></World>
          <h5>我是備用插槽內(nèi)部顯示的內(nèi)容</h5>
          <Vue slot="vue"></Vue>
        </Hello>
    </div>
</template>

Hello.vue 文件

<template>
    <div class="hello">
        <slot name="vue"></slot>
        <h3>我是 hello 組件</h3>
        <slot name="world"></slot>
        <slot></slot>
    </div>
</template>

最后渲染顯示為


三、作用域插槽
1. 作用域插槽是一種特殊類型的插槽,用作一個 (能被傳遞數(shù)據(jù)的) 可重用模板,來代替已經(jīng)渲染好的元素。

在子組件中,slot 插槽還可以用作傳遞數(shù)據(jù),類似于用 prop 來傳遞數(shù)據(jù)

在子組件 Hello.vue 中,只需要將需要傳遞的內(nèi)容寫入插槽中

<template>
    <div class="hello">
        <h3>我是 hello 組件</h3>
        <!-- text 和 text1 是自定義的,可以隨便寫 -->
        <slot text="我是hello" text1="組件的數(shù)據(jù)"></slot>
    </div>
</template>

<slot text="我是hello" text1="組件的數(shù)據(jù)"></slot> 相當于被渲染為

{
    text:'我是hello',
    text1:'組件的數(shù)據(jù)'
}

在 App.vue 中調(diào)用 Hello 組件時,通過在 template 中寫入 slot-scope 特殊屬性來獲取數(shù)據(jù)

<template>
    <div class="app">
        <Hello>
          <!-- template 是該作用域插槽的模版, slot-scope是一個特殊屬性,用來接收 hello 組件中插槽傳過來的值 -->
          <template slot-scope="hello">
            <!-- 通過訪問一個 json 的方式訪問 hello 中 slot 傳過來的值 -->
            <span>{{hello.text}} + {{hello.text1}}</span>
          </template>
        </Hello>
    </div>
</template>

注意:在 vue 2.5.0以上版本,就不需要用到 template 模板了,slot-scope 可以作用在任意元素中

被渲染的結(jié)果為:



2. 更典型的用例是在列表組件中,允許使用者自定義如何渲染列表的每一項

在 App.vue 組件內(nèi)

<template>
    <div class="app">
      <!-- 在子組件綁定 items 把父組件的數(shù)據(jù)傳給子組件 -->
        <Hello :items="items">
          <li slot="item" slot-scope="bbb">
            {{ bbb.text }}
          </li>
        </Hello>
    </div>
</template>

<script>
// 引入 Hello 組件
import Hello from './assets/components/Hello.vue'
export default {
  data(){
    return {
      items:[
        {text:'aaaaaa'},
        {text:'bbbbb'}
      ]
    }
  },
  // 注冊 Hello 組件
  components:{
    Hello
  }
}
</script>

在 Hello.vue 組件中

<template>
    <div class="hello">
        <ul>
            <!-- 此處的 items 為父組件傳過來的數(shù)據(jù) -->
            <!-- text 為 item 內(nèi)的對應數(shù)據(jù),因為 items 是一個對象,所以要用到綁定,否則頁面上直接渲染出來的內(nèi)容為 item.text -->
            <slot name="item" v-for="item in items" text="item.text"></slot>
        </ul>
    </div>
</template>

<script>
export default {
    // 接收父組件傳過來的 items 數(shù)據(jù)
    props: ['items'],
}
</script>

渲染結(jié)果為:


下一篇:Vuex 入門

最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

  • 這篇筆記主要包含 Vue 2 不同于 Vue 1 或者特有的內(nèi)容,還有我對于 Vue 1.0 印象不深的內(nèi)容。關于...
    云之外閱讀 5,177評論 0 29
  • 此文基于官方文檔,里面部分例子有改動,加上了一些自己的理解 什么是組件? 組件(Component)是 Vue.j...
    陸志均閱讀 3,947評論 5 14
  • 本文章是我最近在公司的一場內(nèi)部分享的內(nèi)容。我有個習慣就是每次分享都會先將要分享的內(nèi)容寫成文章。所以這個文集也是用來...
    Awey閱讀 9,575評論 4 67
  • 得到APP 劉潤專欄文章總結(jié)筆記 -->TOP1概念: 看的見的手(政府):凱恩斯認為,經(jīng)濟也得需要國家調(diào)控這只“...
    瑾蘭閱讀 1,441評論 0 2
  • 前段時間在朋友圈發(fā)了阿德萊德粉色湖泊的照片,引得很多寶寶想前去觀看。而網(wǎng)上關于粉湖的資料比較少,所以打算寫一篇攻略...
    米婭的旅程閱讀 415評論 0 0

友情鏈接更多精彩內(nèi)容