vue組件詳解之使用slot分發(fā)內(nèi)容
一、什么是slot
在使用組件時,我們常常要像這樣組合它們:
<app> <app-header></app-header> <app-footer></app-footer> </app>
當需要讓組件組合使用,混合父組件的內(nèi)容與子組件的模板時,就會用到slot , 這個過程叫作內(nèi)容分發(fā)( transclusion )。
注意兩點:
1.< app>組件不知道它的掛載點會有什么內(nèi)容。掛載點的內(nèi)容是由<app >的父組件決定的。
2.<app> 組件很可能有它自己的模板。
props 傳遞數(shù)據(jù)、events 觸發(fā)事件和slot 內(nèi)容分發(fā)就構(gòu)成了Vue 組件的3 個API 來源,再復(fù)雜的組件也是由這3 部分構(gòu)成的。
二、作用域
<child-component>
{{ message }}
</child-component>
這里的message 就是一個slot ,但是它綁定的是父組件的數(shù)據(jù),而不是組件<child-component>的數(shù)據(jù)。
父組件模板的內(nèi)容是在父組件作用域內(nèi)編譯,子組件模板的內(nèi)容是在子組件作用域內(nèi)編譯。如:
<div id="app15">
<child-component v-show="showChild"></child-component>
</div>
Vue.component('child-component',{
template: '<div>子組件</div>'
});
var app15 = new Vue({
el: '#app15',
data: {
showChild: true
}
});
這里的狀態(tài)showChild 綁定的是父組件的數(shù)據(jù),如果想在子組件上綁定,那應(yīng)該是:
<div id="app15">
<child-component></child-component>
</div>
Vue.component('child-component',{
template: '<div v-show="showChild">子組件</div>',
data: function(){
return {
showChild: true
}
}
});
因此, slot 分發(fā)的內(nèi)容,作用域是在父組件上的。
三、slot用法
3.1 單個slot
在子組件內(nèi)使用特殊的<slot>元素就可以為這個子組件開啟一個slot(插槽),在父組件模板里,插入在子組件標簽內(nèi)的所有內(nèi)容將替代子組件的<slot> 標簽及它的內(nèi)容。
<div id="app16">
<my-component16>
<p>分發(fā)的內(nèi)容</p>
<p>更多分發(fā)的內(nèi)容</p>
</my-component16>
</div>
Vue.component('my-component16',{
template: '<div>' +
'<slot><p>如果父組件沒有插入內(nèi)容,我將作為默認出現(xiàn)<</p></slot>' + //預(yù)留的slot插槽
'</div>'
});
var app16 = new Vue({
el: '#app16'
});
渲染結(jié)果為:
<div id=”app16”>
<div>
<p>分發(fā)的內(nèi)容<p>
<p>更多分發(fā)的內(nèi)容<p>
</div>
</div>
子組件child-component 的模板內(nèi)定義了一個<slot>元素,并且用一個<p>作為默認的內(nèi)容,在父組件沒有使用slot 時,會渲染這段默認的文本;如果寫入了slot, 那就會替換整個<slot> 。
3.2具名slot
給<slot> 元素指定一個name 后可以分發(fā)多個內(nèi)容,具名Slot 可以與單個slot 共存。
<div id="app17">
<my-component17>
<h3 slot="header">標題</h3>
<p>正文內(nèi)容</p>
<p>更多正文內(nèi)容</p>
<h3 slot="footer">底部信息</h3>
</my-component17>
</div>
Vue.component('my-component17',{
template: '<div class="container">' +
'<div class="header">' +
'<slot name="header"></slot>' +
'</div>' +
'<div class="main">' +
'<slot></slot>' +
'</div>'+
'<div class="footer">' +
'<slot name="footer"></slot>' +
'</div>'+
'</div>'
});
var app17 = new Vue({
el: '#app17'
});
渲染結(jié)果為:
<div id="app17">
<div class="container">
<div class="header">
<h3>標題</h3></div>
<div class="main">
<p>正文內(nèi)容</p>
<p>更多正文內(nèi)容</p>
</div>
<div class="footer">
<h3>底部信息</h3>
</div>
</div>
</div>
子組件內(nèi)聲明了3 個<s lot>元素,其中在<div class=” main >內(nèi)的<slot> 沒有使用name 特性,它將作為默認slot 出現(xiàn),父組件沒有使用slot 特性的元素與內(nèi)容都將出現(xiàn)在這里。
如果沒有指定默認的匿名slot ,父組件內(nèi)多余的內(nèi)容片段都將被拋棄。
四、作用域插槽
作用域插槽是一種特殊的slot ,使用一個可以復(fù)用的模板替換己渲染元素。
看一個例子:
<div id="app18">
<my-component18>
<template scope="props">
<p>來自父組件的內(nèi)容</p>
<p>{{props.msg}}</p>
</template>
</my-component18>
</div>
Vue.component('my-component18',{
template: '<div class="container"><slot msg="來自子組件的內(nèi)容"></slot></div>'
});
var app18 = new Vue({
el: '#app18'
});
觀察子組件的模板,在<slot>元素上有一個類似props 傳遞數(shù)據(jù)給組件的寫法msg=” xxx ”,將數(shù)據(jù)傳到了插槽。
父組件中使用了<template>元素,而且擁有一個scope=”props ”的特性,這里的props只是一個臨時變量,就像v-for= ” item in items 里面的item 一樣,template 內(nèi)可以通過臨時變量props訪問來自子組件插槽的數(shù)據(jù)msg 。
下面看下Vue組件中slot的用法
主要是讓組件的可擴展性更強。
1. 使用匿名slot

2. 給slot加個名字

如果不在有slot的組件里加入任何標簽,slot什么都不會顯示的。
總結(jié)
以上所述是小編給大家介紹的vue組件詳解之使用slot分發(fā)內(nèi)容,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
Vue數(shù)據(jù)更新但頁面沒有更新的多種情況問題及解決
這篇文章主要介紹了Vue數(shù)據(jù)更新但頁面沒有更新的多種情況問題及解決,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-07-07
Element實現(xiàn)表格分頁數(shù)據(jù)選擇+全選所有完善批量操作
這篇文章主要介紹了Element實現(xiàn)表格分頁數(shù)據(jù)選擇+全選所有完善批量操作,文中通過示例代碼介紹的非常詳細,對大家學習或者使用vue具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧2019-06-06
尤雨溪開發(fā)vue?dev?server理解vite原理
這篇文章主要為大家介紹了尤雨溪開發(fā)的玩具vite,vue-dev-server來理解vite原理,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-07-07

