溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

slot內(nèi)容分發(fā)的使用

發(fā)布時間:2020-06-20 13:03:34 來源:網(wǎng)絡(luò) 閱讀:547 作者:xxxpjgl 欄目:web開發(fā)

一、定義了一個組件custom,該組件本身已經(jīng)具備template模板了,直接調(diào)用<custom></custom>即可渲染模板

<div id="app">
    <custom></custom>
</div>
<script>
    Vue.component('custom',{
        template:`
            <div class="customStyle">
                <p>custom組件內(nèi)容一</p>
                <p>custom組件內(nèi)容二</p>
                <p>custom組件內(nèi)容三</p>
            </div>
        `
    })
    new Vue({
        el:'#app'
    })
</script>

二、匿名插槽
現(xiàn)在,在使用組件custom的同時,想替換這個組件默認已經(jīng)定義好的模板,就可以使用slot內(nèi)容分發(fā)
用法:
在<custom></custom>內(nèi)部寫入一些希望展示的html模板,比如
<custom><div>我是自定義的模板</div></custom>,然后,將組件template用<slot></slot>包裹起來,即:

<div id="app">
    <custom><div>我是自定義的模板</div></custom>
</div>
<script>
    Vue.component('custom',{
        template:`
            <div class="customStyle">
                <slot>
                    <p>custom組件內(nèi)容一</p>
                    <p>custom組件內(nèi)容二</p>
                    <p>custom組件內(nèi)容三</p>
                </slot>
            </div>
        `
    })
    new Vue({
        el:'#app'
    })
</script>

那么,
當(dāng)在custom標(biāo)簽內(nèi)有自定義的模板時,那么就會替代slot內(nèi)部的模板內(nèi)容,渲染到頁面
而當(dāng)在custom標(biāo)簽內(nèi)沒有自定義的模板,那么就會渲染slot內(nèi)部的模板內(nèi)容

這就是匿名插槽,不用設(shè)置名稱屬性name,單個插槽可以放置在組件的任意位置,但是就像它的名字一樣,一個組件中只能有一個該類插槽。相對應(yīng)的,具名插槽就可以有很多個,只要名字(名稱屬性)不同就可以了。
slot內(nèi)容分發(fā)的使用

二、具名插槽
在custom標(biāo)簽內(nèi)有自定義的模板,數(shù)量很多,想讓custom標(biāo)簽內(nèi)某部分的模板渲染到,組件內(nèi)部對應(yīng)的位置時,就使用具名插槽了

<div id="app">
    <custom>

        <div slot="one">替換組價內(nèi)容一</div>
        <div slot="three">替換組價內(nèi)容三</div>

        <template slot="two">  //當(dāng)自定義的模板內(nèi)容很多時,就可以使用template括起來,寫上slot
            <div>替換組價內(nèi)容二</div>
            <div>替換組價內(nèi)容二</div>
            <div>替換組價內(nèi)容二</div>
            <div>替換組價內(nèi)容二</div>
            <div>替換組價內(nèi)容二</div>
        </template>

        <div>替換無名的slot</div>  //沒寫slot屬性值時,就默認替換slot沒有name值的那個模板內(nèi)容

    </custom>
</div>
<script>
    Vue.component('custom',{
        template:`
            <div class="customStyle">

                    <slot name="one><p">custom組件內(nèi)容一</p></slot>
                    <slot name="two"><p>custom組件內(nèi)容二</p></slot>
                    <slot name="three"><p>custom組件內(nèi)容三</p></slot>

                    <slot><p>我是無名的slot</p></slot>

            </div>
        `
    })
    new Vue({
        el:'#app'
    })
</script>

slot內(nèi)容分發(fā)的使用

三、編譯作用域
slot內(nèi)容分發(fā)的使用
<div id="app"> //id為app所在的區(qū)域都屬于父組件
<custom> //這是父組件,所以這個message渲染的是父組件里的message
{{message}}
</custom>
</div>
<script>
Vue.component('custom',{
data(){
return {
message:'我是子組件的數(shù)據(jù)'
}
},
template:`
<div class="customStyle">
{{message}} //這是子組件,所以這個message渲染的是子組件里的message
<h3>我是默認的永遠都顯示的模板內(nèi)容</h3>
<slot></slot>

        </div>
    `
})
new Vue({
    el:'#app',
    data:{
        message:'我是父組件的數(shù)據(jù)'
    }
})

</script>



四、總結(jié):匿名插槽:看到自定義組件內(nèi)容有模板時,直接聯(lián)想到可以替換組件定義時template的<slot></slot>里的內(nèi)容,如果模板內(nèi)容沒有slot包裹,則默認全部永遠都顯示(只要調(diào)用了這個組件)
具名插槽:看到自定義組件內(nèi)容內(nèi)的模板有slot屬性值,則和組件定義時template的<slot></slot>上的name值一一對應(yīng)
向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI