slot內(nèi)容分發(fā)的使用-創(chuàng)新互聯(lián)

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

寧縣網(wǎng)站建設(shè)公司成都創(chuàng)新互聯(lián),寧縣網(wǎng)站設(shè)計制作,有大型網(wǎng)站制作公司豐富經(jīng)驗。已為寧縣上1000+提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\外貿(mào)網(wǎng)站制作要多少錢,請找那個售后服務(wù)好的寧縣做網(wǎng)站的公司定做!
<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的同時,想替換這個組件默認(rèn)已經(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屬性值時,就默認(rèn)替換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>我是默認(rèn)的永遠(yuǎn)都顯示的模板內(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包裹,則默認(rèn)全部永遠(yuǎn)都顯示(只要調(diào)用了這個組件)
具名插槽:看到自定義組件內(nèi)容內(nèi)的模板有slot屬性值,則和組件定義時template的<slot></slot>上的name值一一對應(yīng)

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點(diǎn)與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。

文章題目:slot內(nèi)容分發(fā)的使用-創(chuàng)新互聯(lián)
本文鏈接:http://muchs.cn/article24/dscjce.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計公司、App開發(fā)網(wǎng)站策劃、網(wǎng)站制作移動網(wǎng)站建設(shè)、網(wǎng)站營銷

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站建設(shè)