插槽

? 1、作用:讓父組件可以向子組件指定位置插入html結(jié)構(gòu),也是一種組件之間通信的方式,

???????????????????? 適用于 父組件===>子組件。

? 2、分類:默認(rèn)插槽、具名插槽、作用域插槽

? 3、使用方式:

? ? ? 1、默認(rèn)插槽:?

? ? ? ? 父組件中:

? ? ? ? ? ? <Category>

? ? ? ? ? ? ? <div>html結(jié)構(gòu)</div>

? ? ? ? ? ? </Category>

? ? ? ? 子組件中:

? ? ? ? ? ? <template>

? ? ? ? ? ? ? <div>

? ? ? ? ? ? ? ? ? //定義一個(gè)插槽

? ? ? ? ? ? ? ? ? <slot>插槽默認(rèn)內(nèi)容</slot>

? ? ? ? ? ? ? </div>

? ? ? ? ? ? </template>


? ? ? 2、具名插槽:

? ? ? ? 父組件中:

? ? ? ? ? <Category>

? ? ? ? ? ? <template slot="center">?????? //當(dāng)多個(gè)插槽時(shí) 選擇該結(jié)構(gòu)放入哪一個(gè)插槽

? ? ? ? ? ? ? ?? <div>html結(jié)構(gòu)1</div>

? ? ? ? ? ? </template>

? ? ? ? ? ? <template v-slot:footer>??? //當(dāng)用到template時(shí) 在vue2.6中有新寫法

? ? ? ? ? ? ??????? <div>html結(jié)構(gòu)2</div>

? ? ? ? ? ? </template>


//如果沒有template。直接在結(jié)構(gòu)上寫slot=“center”

? ? ? ? ? </Category>

? ? ? ? 子組件中:

? ? ? ? ? ? <template>

? ? ? ? ? ? ? <div>

? ? ? ? ? ? ? ? //定義插槽

? ? ? ? ? ? ? ? <slot name="center"></slot>

? ? ? ? ? ? ? ? <slot name="footer"></slot>

? ? ? ? ? ? ? </div>

? ? ? ? ? ? </template>

? ? ? 3、作用域插槽

? ? ? ? 1、理解:數(shù)據(jù)在組件的自身,但根據(jù)數(shù)據(jù)生成的結(jié)構(gòu)需要組件的使用者來決定。(games數(shù)據(jù)在Category中,但使用數(shù)據(jù)所遍歷出來的結(jié)構(gòu)由

? ? ? ? App組件來決定)

? ? ? ? 2、具體編碼:

<slot :games="games"></slot>??? //把數(shù)據(jù)傳給插槽

//想要拿到插槽傳入的games數(shù)據(jù)?? 必須用template標(biāo)簽

? 接受數(shù)據(jù) scope=“{games}”?

?????? 新寫法:slot-scope = “{games}”

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容