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

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

寧縣網(wǎng)站建設(shè)公司成都創(chuàng)新互聯(lián),寧縣網(wǎng)站設(shè)計(jì)制作,有大型網(wǎng)站制作公司豐富經(jīng)驗(yàn)。已為寧縣上1000+提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\外貿(mào)網(wǎng)站制作要多少錢(qián),請(qǐng)找那個(gè)售后服務(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的同時(shí),想替換這個(gè)組件默認(rèn)已經(jīng)定義好的模板,就可以使用slot內(nèi)容分發(fā)
用法:
在<custom></custom>內(nèi)部寫(xiě)入一些希望展示的html模板,比如
<custom><div>我是自定義的模板</div></custom>,然后,將組件template用<slot></slot>包裹起來(lái),即:

<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)有自定義的模板時(shí),那么就會(huì)替代slot內(nèi)部的模板內(nèi)容,渲染到頁(yè)面
而當(dāng)在custom標(biāo)簽內(nèi)沒(méi)有自定義的模板,那么就會(huì)渲染slot內(nèi)部的模板內(nèi)容

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

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

<div id="app">
    <custom>

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

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

        <div>替換無(wú)名的slot</div>  //沒(méi)寫(xiě)slot屬性值時(shí),就默認(rèn)替換slot沒(méi)有name值的那個(gè)模板內(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>我是無(wú)名的slot</p></slot>

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

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

三、編譯作用域
slot內(nèi)容分發(fā)的使用
<div id="app"> //id為app所在的區(qū)域都屬于父組件
<custom> //這是父組件,所以這個(gè)message渲染的是父組件里的message
{{message}}
</custom>
</div>
<script>
Vue.component('custom',{
data(){
return {
message:'我是子組件的數(shù)據(jù)'
}
},
template:`
<div class="customStyle">
{{message}} //這是子組件,所以這個(gè)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)容有模板時(shí),直接聯(lián)想到可以替換組件定義時(shí)template的<slot></slot>里的內(nèi)容,如果模板內(nèi)容沒(méi)有slot包裹,則默認(rèn)全部永遠(yuǎn)都顯示(只要調(diào)用了這個(gè)組件)
具名插槽:看到自定義組件內(nèi)容內(nèi)的模板有slot屬性值,則和組件定義時(shí)template的<slot></slot>上的name值一一對(duì)應(yīng)

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

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

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

廣告

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

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