2019獨角獸企業重金招聘Python工程師標準>>>
①概述:
簡單來說,假如父組件需要在子組件內放一些DOM,那么這些DOM是顯示、不顯示、在哪個地方顯示、如何顯示,就是slot分發負責的活。
?
②默認情況下
父組件在子組件內套的內容,是不顯示的。
例如代碼:
<div id="app"> <children> <span>12345</span> <!--上面這行不會顯示--> </children>
</div>
<script> var vm = new Vue({ el: '#app', components: { children: { //這個無返回值,不會繼續派發 template: "<button>為了明確作用范圍,所以使用button標簽</button>" } } });
</script>
?
顯示內容是一個button按鈕,不包含span標簽里面的內容;
?
?
③單個slot
簡單來說,只使用這個標簽的話,可以將父組件放在子組件的內容,放到想讓他顯示的地方。
<div id="app"> <children> <span>12345</span> <!--上面這行不會顯示--> </children>
</div>
<script> var vm = new Vue({ el: '#app', components: { children: { //這個無返回值,不會繼續派發 template: "<button><slot></slot>為了明確作用范圍,所以使用button標簽</button>" } } });
</script>
?
例如這樣寫的話,結果是:
<button><span>12345</span>為了明確作用范圍,所以使用button標簽</button>
?
即父組件放在子組件里的內容,插到了子組件的<slot></slot>位置;
注意,即使有多個標簽,會一起被插入,相當于用父組件放在子組件里的標簽,替換了<slot></slot>這個標簽。
?
?
④具名slot
將放在子組件里的不同html標簽放在不同的位置
父組件在要分發的標簽里添加?slot=”name名”?屬性
子組件在對應分發的位置的slot標簽里,添加name=”name名”?屬性,
然后就會將對應的標簽放在對應的位置了。
?
示例代碼:
<div id="app"> <children> <span slot="first">12345</span> <span slot="second">56789</span> <!--上面這行不會顯示--> </children>
</div>
<script> var vm = new Vue({ el: '#app', components: { children: { //這個無返回值,不會繼續派發 template: "<button><slot name='first'></slot>為了明確作用范圍,<slot name='second'></slot>所以使用button標簽</button>" } } });
</script>
?
顯示結果為:(為了方便查看,已手動調整換行)
<button><span slot="first">12345</span>為了明確作用范圍,<span slot="second">56789</span>所以使用button標簽</button>
?
⑤分發內容的作用域:
被分發的內容的作用域,根據其所在模板決定,例如,以上標簽,其在父組件的模板中(雖然其被子組件的children標簽所包括,但由于他不在子組件的template屬性中,因此不屬于子組件),則受父組件所控制。
?
示例代碼:
<div id="app"> <children> <span slot="first" @click="tobeknow">12345</span> <span slot="second">56789</span> <!--上面這行不會顯示--> </children>
</div>
<script> var vm = new Vue({ el: '#app', methods: { tobeknow: function () { console.log("It is the parent's method"); } }, components: { children: { //這個無返回值,不會繼續派發 template: "<button><slot name='first'></slot>為了明確作用范圍,<slot name='second'></slot>所以使用button標簽</button>" } } });
</script>
?
當點擊文字12345的區域時(而不是按鈕全部),會觸發父組件的tobeknow方法。
?
但是點擊其他區域時則沒有影響。
?
官方教程是這么說的:
父組件模板的內容在父組件作用域內編譯;子組件模板的內容在子組件作用域內編譯
?
?
?
⑥當沒有分發內容時的提示:
假如父組件沒有在子組件中放置有標簽,或者是父組件在子組件中放置標簽,但有slot屬性,而子組件中沒有該slot屬性的標簽。
?
那么,子組件的slot標簽,將不會起到任何作用。
?
除非,該slot標簽內有內容,那么在無分發內容的時候,會顯示該slot標簽內的內容。
?
如示例代碼:
<div id="app"> <children> <span slot="first">【12345】</span> <!--上面這行不會顯示--> </children>
</div>
<script> var vm = new Vue({ el: '#app', components: { children: { //這個無返回值,不會繼續派發 template: "<div><slot name='first'><button>【如果沒有內容則顯示我1】</button></slot>為了明確作用范圍,<slot name='last'><button>【如果沒有內容則顯示我2】</button></slot>所以使用button標簽</div>" } } });
</script>
說明:
【1】name=’first’的slot標簽被父組件對應的標簽所替換(slot標簽內部的內容被舍棄);
【2】name=’last’的slot標簽,因為沒有對應的內容,則顯示該slot標簽內部的內容。
?
?
?
⑦假如想控制子組件根標簽的屬性
【1】首先,由于模板標簽是屬于父組件的,因此,將子組件的指令綁定在模板標簽里,是不可以的(因為他歸屬于父組件);
?
【2】假如需要通過父組件控制子組件是否顯示(例如v-if或者v-show),那么這個指令顯然是屬于父組件的(例如放在父組件的data下面)。可以將標簽寫在子組件的模板上。
如代碼:
<div id="app"> <button @click="toshow">點擊讓子組件顯示</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: { //這個無返回值,不會繼續派發 template: "<div>這里是子組件</div>" } } });
</script>
?
說明:
通過父組件(點擊按鈕,切換v-if指令的值)控制子組件是否顯示。
?
?
【3】假如需要通過子組件,控制子組件是否顯示(比如讓他隱藏),那么這個指令顯然是屬于子組件的(會將值放在子組件的data屬性下),那么就不能像上面這么寫,而是必須放置在子組件的根標簽中。
<div id="app"> <button @click="toshow">點擊讓子組件顯示</button> <children> <span slot="first">【12345】</span> <!--上面這行不會顯示--> </children>
</div>
<script> var vm = new Vue({ el: '#app', methods: { toshow: function () { this.$children[0].tohidden = true; } }, components: { children: { //這個無返回值,不會繼續派發 template: "<div v-if='tohidden' @click='tohide'>這里是子組件</div>", data: function () { return { tohidden: true } }, methods: { tohide: function () { this.tohidden = !this.tohidden; } } } } });
</script>
說明:
點擊子組件會讓子組件消失;
點擊父組件的按鈕,通過更改子組件的tohidden屬性,讓子組件重新顯示。
子組件的指令綁定在子組件的模板之中(如此才能調用);