vue插槽 slot

vue插槽 slot

Vue 實(shí)現(xiàn)了一套內(nèi)容分發(fā)的 API,這套 API 的設(shè)計(jì)靈感源自 Web Components 規(guī)范草案,將 <slot> 元素作為承載分發(fā)內(nèi)容的出口。
vue文檔

子組件:

<template>
  <div>
      <h1>我是seo組件</h1>
      <!-- header -->
      <slot name="header" :row='obj'>
        
      </slot>
      <!-- middle 匿名插槽-->
      <slot >
        
      </slot>
      <!-- footer -->
      <slot name="footer" :row='obj'>
        
      </slot>
  </div>
</template>

<script>
export default {
    data(){
        return{
            obj:{
                header:"頭部?jī)?nèi)容",
                footer:"尾部?jī)?nèi)容"
            }
        }
    }
}
</script>

slot中name屬性和父組件中v-slot:后面跟的參數(shù)一致,則會(huì)讓父組件中的template內(nèi)容在這展示。即通過具名來判斷,父組件的內(nèi)容分發(fā)到那一塊。
由于組件之間作用域是獨(dú)立的,如果想在父組件中用到子組件的數(shù)據(jù),則需要用到作用域插槽。

自 2.6.0 起有所更新。已廢棄的使用 slot-scope attribute 的語法。但是依舊可以使用,直到Vue3.0中廢棄。

作用域插槽使用就是在子組件中用屬性綁定在 具名插槽的元素上,然后在父組件中v-slot:(插槽的名字)='scope',scope是自己寫的獲取的變量,可以自行定義。

父組件:

<template>
    <div class="about">
        <h1>This is an about page</h1>
        <seo>
            <!-- footer -->
            <template v-slot:footer="scope">
                <h1>footer</h1>
                <div>
                    {{scope.row.footer}}
                </div>
            </template>
            <!-- header -->
            <template v-slot:header='scope'>
                <h1>title</h1>
                <div>
                    {{scope.row.header}}
                </div>
            </template>
            <!-- middle 非具名插槽都會(huì)顯示在匿名插槽中-->
            <div>
                我是在父組件中的插槽middle
            </div>
            <h1>123</h1>
            
        </seo>
    </div>
</template>

<script>
import seo from "../components/seo"
export default {
    components:{
        seo,
    },
    data(){
        return{
        }
    }

}
</script>

在子組件中定義好位置以后,父組件中內(nèi)容分發(fā),是無所謂位置的。比如我這header部分和footer部分就不是對(duì)應(yīng)位置。

展示效果

預(yù)覽.png

接著我們就可以在父組件中修改子組件的分發(fā)上來的數(shù)據(jù)了。

<template>
    <div class="about">
        <h1>This is an about page</h1>
        <seo>
            <template v-slot:footer="scope">
                <h1>footer</h1>
                <div>
                    <el-select v-model="scope.row.footer">
                        <el-option v-for="item in footer_list" :key="item.id" :label="item.name" :value="item.id">
                        </el-option>
                    </el-select>
                    {{scope.row.footer}}
                </div>
            </template>
            <template v-slot:header='scope'>
                <h1>title</h1>
                <div>
                    {{scope.row.header}}
                </div>
            </template>
            <div>
                我是在父組件中的插槽middle
            </div>
            <h1>123</h1>
            
        </seo>
    </div>
</template>

<script>
import seo from "../components/seo"
export default {
    components:{
        seo,
    },
    data(){
        return{
            footer_list:[
                {id:1,name:'1底部43'},
                {id:2,name:'2底部93'},
            ]
        }
    }

}
</script>
預(yù)覽2.png

當(dāng)我選擇下拉選擇‘1底部43’


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

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

  • 什么插槽 官網(wǎng)說明: 在 2.6.0中,我們?yōu)榫呙宀酆妥饔糜虿宀垡肓艘粋€(gè)新的統(tǒng)一的語法 (即 v-slot 指...
    全球頂尖偽極客閱讀 2,459評(píng)論 0 0
  • By Leaf 在學(xué)習(xí)的過程中遇到了Vue的slot元素,開始不知道也不了解它的用途、用法,即然遇到了不懂的知識(shí)點(diǎn)...
    HYC_閱讀 5,176評(píng)論 7 42
  • Vue 實(shí)現(xiàn)了一套內(nèi)容分發(fā)的 API,將 元素作為承載分發(fā)內(nèi)容的出口。大致如下parent為父組件,child為...
    Kevin_hoo閱讀 340評(píng)論 0 0
  • 一、概念解析 插槽slot:插槽有點(diǎn)像組件中的一個(gè)占位符,當(dāng)別的組件使用這個(gè)組件的時(shí)候,在這一對(duì)標(biāo)簽中又寫了其他內(nèi)...
    klmhly閱讀 602評(píng)論 0 0
  • 副標(biāo)題:愛是成全對(duì)方 A.挑戰(zhàn): 讓母親愛上"斷舍離"舊衣服 B.成見: 1,母親年齡大,而且,非常節(jié)儉,需要改變...
    花道小子閱讀 568評(píng)論 0 0

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