什么是組件?
組件(Component)是 Vue.js 最強(qiáng)大的功能之一。組件可以擴(kuò)展 HTML 元素,封裝可重用的代碼。在較高層面上,組件是自定義元素,Vue.js 的編譯器為它添加特殊功能。在有些情況下,組件也可以是原生 HTML 元素的形式,以 is 特性擴(kuò)展。
Slot分發(fā)內(nèi)容
①概述:
簡(jiǎn)單來(lái)說(shuō),假如父組件需要在子組件內(nèi)放一些DOM,那么這些DOM是顯示、不顯示、在哪個(gè)地方顯示、如何顯示,就是slot分發(fā)負(fù)責(zé)的活。
②默認(rèn)情況下
父組件在子組件內(nèi)套的內(nèi)容,是不顯示的。
例如代碼:
<div id="app"> <children> <span>12345</span> <!--上面這行不會(huì)顯示--> </children> </div> <script> var vm = new Vue({ el: '#app', components: { children: { //這個(gè)無(wú)返回值,不會(huì)繼續(xù)派發(fā) template: "<button>為了明確作用范圍,所以使用button標(biāo)簽</button>" } } }); </script> 顯示內(nèi)容是一個(gè)button按鈕,不包含span標(biāo)簽里面的內(nèi)容;
③單個(gè)slot
簡(jiǎn)單來(lái)說(shuō),只使用這個(gè)標(biāo)簽的話,可以將父組件放在子組件的內(nèi)容,放到想讓他顯示的地方。
<div id="app"> <children> <span>12345</span> <!--上面這行不會(huì)顯示--> </children> </div> <script> var vm = new Vue({ el: '#app', components: { children: { //這個(gè)無(wú)返回值,不會(huì)繼續(xù)派發(fā) template: "<button><slot></slot>為了明確作用范圍,所以使用button標(biāo)簽</button>" } } }); </script> 例如這樣寫(xiě)的話,結(jié)果是:
<button><span>12345</span>為了明確作用范圍,所以使用button標(biāo)簽</button>
即父組件放在子組件里的內(nèi)容,插到了子組件的<slot></slot>位置;
注意,即使有多個(gè)標(biāo)簽,會(huì)一起被插入,相當(dāng)于用父組件放在子組件里的標(biāo)簽,替換了<slot></slot>這個(gè)標(biāo)簽。
④具名slot
將放在子組件里的不同html標(biāo)簽放在不同的位置
父組件在要分發(fā)的標(biāo)簽里添加 slot=”name名” 屬性
子組件在對(duì)應(yīng)分發(fā)的位置的slot標(biāo)簽里,添加name=”name名” 屬性,然后就會(huì)將對(duì)應(yīng)的標(biāo)簽放在對(duì)應(yīng)的位置了。
示例代碼:
<div id="app"> <children> <span slot="first">12345</span> <span slot="second">56789</span> <!--上面這行不會(huì)顯示--> </children> </div> <script> var vm = new Vue({ el: '#app', components: { children: { //這個(gè)無(wú)返回值,不會(huì)繼續(xù)派發(fā) template: "<button><slot name='first'></slot>為了明確作用范圍,<slot name='second'></slot>所以使用button標(biāo)簽</button>" } } }); </script>顯示結(jié)果為:(為了方便查看,已手動(dòng)調(diào)整換行)
<button><span slot="first">12345</span>為了明確作用范圍,<span slot="second">56789</span>所以使用button標(biāo)簽</button>
⑤分發(fā)內(nèi)容的作用域:
被分發(fā)的內(nèi)容的作用域,根據(jù)其所在模板決定,例如,以上標(biāo)簽,其在父組件的模板中(雖然其被子組件的children標(biāo)簽所包括,但由于他不在子組件的template屬性中,因此不屬于子組件),則受父組件所控制。
示例代碼:
<div id="app"> <children> <span slot="first" @click="tobeknow">12345</span> <span slot="second">56789</span> <!--上面這行不會(huì)顯示--> </children> </div> <script> var vm = new Vue({ el: '#app', methods: { tobeknow: function () { console.log("It is the parent's method"); } }, components: { children: { //這個(gè)無(wú)返回值,不會(huì)繼續(xù)派發(fā) template: "<button><slot name='first'></slot>為了明確作用范圍,<slot name='second'></slot>所以使用button標(biāo)簽</button>" } } }); </script>當(dāng)點(diǎn)擊文字12345的區(qū)域時(shí)(而不是按鈕全部),會(huì)觸發(fā)父組件的tobeknow方法。
但是點(diǎn)擊其他區(qū)域時(shí)則沒(méi)有影響。
官方教程是這么說(shuō)的:
父組件模板的內(nèi)容在父組件作用域內(nèi)編譯;子組件模板的內(nèi)容在子組件作用域內(nèi)編譯
⑥當(dāng)沒(méi)有分發(fā)內(nèi)容時(shí)的提示:
假如父組件沒(méi)有在子組件中放置有標(biāo)簽,或者是父組件在子組件中放置標(biāo)簽,但有slot屬性,而子組件中沒(méi)有該slot屬性的標(biāo)簽。
那么,子組件的slot標(biāo)簽,將不會(huì)起到任何作用。
除非,該slot標(biāo)簽內(nèi)有內(nèi)容,那么在無(wú)分發(fā)內(nèi)容的時(shí)候,會(huì)顯示該slot標(biāo)簽內(nèi)的內(nèi)容。
如示例代碼:
<div id="app"> <children> <span slot="first">【12345】</span> <!--上面這行不會(huì)顯示--> </children> </div> <script> var vm = new Vue({ el: '#app', components: { children: { //這個(gè)無(wú)返回值,不會(huì)繼續(xù)派發(fā) template: "<div><slot name='first'><button>【如果沒(méi)有內(nèi)容則顯示我1】</button></slot>為了明確作用范圍,<slot name='last'><button>【如果沒(méi)有內(nèi)容則顯示我2】</button></slot>所以使用button標(biāo)簽</div>" } } }); </script> 說(shuō)明:
【1】name='first'的slot標(biāo)簽被父組件對(duì)應(yīng)的標(biāo)簽所替換(slot標(biāo)簽內(nèi)部的內(nèi)容被舍棄);
【2】name='last'的slot標(biāo)簽,因?yàn)闆](méi)有對(duì)應(yīng)的內(nèi)容,則顯示該slot標(biāo)簽內(nèi)部的內(nèi)容。
⑦假如想控制子組件根標(biāo)簽的屬性
【1】首先,由于模板標(biāo)簽是屬于父組件的,因此,將子組件的指令綁定在模板標(biāo)簽里,是不可以的(因?yàn)樗麣w屬于父組件);
【2】假如需要通過(guò)父組件控制子組件是否顯示(例如v-if或者v-show),那么這個(gè)指令顯然是屬于父組件的(例如放在父組件的data下面)。可以將標(biāo)簽寫(xiě)在子組件的模板上。
如代碼:
<div id="app"> <button @click="toshow">點(diǎn)擊讓子組件顯示</button> <children v-if="abc"> </children> </div> <script> var vm = new Vue({ el: '#app', data: { abc: false }, methods: { toshow: function () { this.abc = !this.abc; } }, components: { children: { //這個(gè)無(wú)返回值,不會(huì)繼續(xù)派發(fā) template: "<div>這里是子組件</div>" } } }); </script>說(shuō)明:
通過(guò)父組件(點(diǎn)擊按鈕,切換v-if指令的值)控制子組件是否顯示。
【3】假如需要通過(guò)子組件,控制子組件是否顯示(比如讓他隱藏),那么這個(gè)指令顯然是屬于子組件的(會(huì)將值放在子組件的data屬性下),那么就不能像上面這么寫(xiě),而是必須放置在子組件的根標(biāo)簽中。
<div id="app"> <button @click="toshow">點(diǎn)擊讓子組件顯示</button> <children> <span slot="first">【12345】</span> <!--上面這行不會(huì)顯示--> </children> </div> <script> var vm = new Vue({ el: '#app', methods: { toshow: function () { this.$children[0].tohidden = true; } }, components: { children: { //這個(gè)無(wú)返回值,不會(huì)繼續(xù)派發(fā) template: "<div v-if='tohidden' @click='tohide'>這里是子組件</div>", data: function () { return { tohidden: true } }, methods: { tohide: function () { this.tohidden = !this.tohidden; } } } } }); </script>說(shuō)明:
點(diǎn)擊子組件會(huì)讓子組件消失;
點(diǎn)擊父組件的按鈕,通過(guò)更改子組件的tohidden屬性,讓子組件重新顯示。
子組件的指令綁定在子組件的模板之中(如此才能調(diào)用);
以上所述是小編給大家介紹的Vuejs第十一篇組件之slot內(nèi)容分發(fā)實(shí)例詳解,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)VeVb武林網(wǎng)網(wǎng)站的支持!
新聞熱點(diǎn)
疑難解答