vue.js 中slot 用處大(轉載)

來源:互聯網
上載者:User

標籤:abc   綁定   而不是   不包含   地方   特殊功能   換行   隱藏   second   

什麼是組件?

組件(Component)是 Vue.js 最強大的功能之一。組件可以擴充 HTML 元素,封裝可重用的代碼。在較高層面上,組件是自訂元素,Vue.js 的編譯器為它添加特殊功能。在有些情況下,組件也可以是原生 HTML 元素的形式,以 is 特性擴充。

Slot分發內容

①概述:

簡單來說,假如父組件需要在子組件內放一些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屬性,讓子組件重新顯示。

子組件的指令綁定在子組件的模板之中(如此才能調用);

vue.js 中slot 用處大(轉載)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.