本文為轉(zhuǎn)載,原文:Vue學(xué)習(xí)筆記入門篇——組件的內(nèi)容分發(fā)(slot)
介紹
為了讓組件可以組合,我們需要一種方式來混合父組件的內(nèi)容與子組件自己的模板。這個(gè)過程被稱為 內(nèi)容分發(fā) (或 “transclusion” 如果你熟悉 Angular)。Vue.js 實(shí)現(xiàn)了一個(gè)內(nèi)容分發(fā) API姊舵,使用特殊的 'slot' 元素作為原始內(nèi)容的插槽。
編譯作用域
在深入內(nèi)容分發(fā) API 之前寓落,我們先明確內(nèi)容在哪個(gè)作用域里編譯括丁。假定模板為:
<child-component>
{{ message }}
</child-component>
message 應(yīng)該綁定到父組件的數(shù)據(jù),還是綁定到子組件的數(shù)據(jù)伶选?答案是父組件史飞。組件作用域簡單地說是:
父組件模板的內(nèi)容在父組件作用域內(nèi)編譯尖昏;
子組件模板的內(nèi)容在子組件作用域內(nèi)編譯。
一個(gè)常見錯(cuò)誤是試圖在父組件模板內(nèi)將一個(gè)指令綁定到子組件的屬性/方法:
<!-- 無效 -->
<child-component v-show="someChildProperty"></child-component>
假定 someChildProperty 是子組件的屬性祸憋,上例不會(huì)如預(yù)期那樣工作会宪。父組件模板不應(yīng)該知道子組件的狀態(tài)。
如果要綁定作用域內(nèi)的指令到一個(gè)組件的根節(jié)點(diǎn)蚯窥,你應(yīng)當(dāng)在組件自己的模板上做:
Vue.component('child-component', {
// 有效,因?yàn)槭窃谡_的作用域內(nèi)
template: '<div v-show="someChildProperty">Child</div>',
data: function () {
return {
someChildProperty: true
}
}
})
類似地塞帐,分發(fā)內(nèi)容是在父作用域內(nèi)編譯拦赠。
單個(gè)slot
除非子組件模板包含至少一個(gè) 'slot' 插口,否則父組件的內(nèi)容將會(huì)被丟棄葵姥。當(dāng)子組件模板只有一個(gè)沒有屬性的 slot 時(shí)荷鼠,父組件整個(gè)內(nèi)容片段將插入到 slot 所在的 DOM 位置,并替換掉 slot 標(biāo)簽本身榔幸。
最初在 'slot' 標(biāo)簽中的任何內(nèi)容都被視為備用內(nèi)容允乐。備用內(nèi)容在子組件的作用域內(nèi)編譯,并且只有在宿主元素為空削咆,且沒有要插入的內(nèi)容時(shí)才顯示備用內(nèi)容牍疏。
示例代碼:
<div id="app">
<h1>我是父組件的標(biāo)題</h1>
<my-component>
<p>初始內(nèi)容1</p>
<p>初始內(nèi)容2</p>
</my-component>
</div>
Vue.component('my-component',{
template:`
<div>
<h2>我是子組件的標(biāo)題</h2>
<slot>
只有在沒有要分發(fā)的內(nèi)容是才出現(xiàn)。
</slot>
<div>
`,
})
new Vue({
el:'#app'
})
運(yùn)行結(jié)果如下:
將html部分代碼修改為以下代碼:
<div id="app">
<h1>我是父組件的標(biāo)題</h1>
<my-component>
</my-component>
</div>
則運(yùn)行結(jié)果如下:
具名slot
'slot' 元素可以用一個(gè)特殊的屬性 name 來配置如何分發(fā)內(nèi)容拨齐。多個(gè) slot 可以有不同的名字鳞陨。具名 slot 將匹配內(nèi)容片段中有對(duì)應(yīng) slot 特性的元素。
仍然可以有一個(gè)匿名 slot瞻惋,它是默認(rèn) slot厦滤,作為找不到匹配的內(nèi)容片段的備用插槽。如果沒有默認(rèn)的 slot歼狼,這些找不到匹配的內(nèi)容片段將被拋棄掏导。
如以下例子:
<div id="app">
<my-component>
<h1 slot="header">這是標(biāo)題</h1>
<p>第一個(gè)段落</p>
<p>第二個(gè)段落</p>
<p>第三個(gè)段落</p>
<p slot="footer">聯(lián)系信息</p>
</my-component>
</div>
Vue.component('my-component',{
template:`
<div class="container">
<header>
<slot name="header"></slot>
</header>
<main>
<slot></slot>
</main>
<footer>
<slot name="footer"></slot>
</footer>
<div>
`,
})
new Vue({
el:'#app'
})
運(yùn)行結(jié)果如下:
在組合組件時(shí),內(nèi)容分發(fā) API 是非常有用的機(jī)制羽峰。
作用域插槽
2.1.0新增
作用域插槽是一種特殊類型的插槽趟咆,用作使用一個(gè) (能夠傳遞數(shù)據(jù)到) 可重用模板替換已渲染元素。
在子組件中限寞,只需將數(shù)據(jù)傳遞到插槽忍啸,就像你將 props 傳遞給組件一樣。
示例代碼:
<div id="app">
<my-component>
<template scope="props">
<p>hello from parent</p>
<p>{{props.text}}</p>
</template>
</my-component>
</div>
Vue.component('my-component',{
template:`
<div class="child">
<slot text="hello from child"></slot>
<div>
`,
props:['text']
})
new Vue({
el:'#app'
})
運(yùn)行結(jié)果:
在父級(jí)中履植,具有特殊屬性 scope 的 <'template'> 元素必須存在计雌,表示它是作用域插槽的模板。scope 的值對(duì)應(yīng)一個(gè)臨時(shí)變量名玫霎,此變量接收從子組件中傳遞的 props 對(duì)象凿滤。
作用域插槽更具代表性的用例是列表組件妈橄,允許組件自定義應(yīng)該如何渲染列表每一項(xiàng):
<div id="app">
<my-component :items="items">
<template slot="item" scope="props">
<li>{{props.text}}</li>
</template>
</my-component>
</div>
Vue.component('my-component',{
template:`
<ul>
<slot name="item" v-for="item in items" :text="item.text"></slot>
</ul>
`,
props:['text','items']
})
new Vue({
el:'#app',
data:{
items:[
{text:'item1'},
{text:'item2'},
{text:'item3'},
]
}
})
作用域插槽也可以是具名的
運(yùn)行結(jié)果:
完
本文為原創(chuàng),轉(zhuǎn)載請注明出處翁脆。
上一節(jié):Vue學(xué)習(xí)筆記入門篇——組件的通訊
返回目錄
下一節(jié):Vue學(xué)習(xí)筆記入門篇——組件雜項(xiàng)