詳解Vue中使用插槽(slot)、聚類插槽
一、基本的插槽
這里總結(jié)兩點(diǎn)
- 如果不在子組件中使用插槽(slot),那么在子組件中寫(xiě)任何代碼都是無(wú)效的的,不會(huì)顯示
- (插槽默認(rèn)值)如果子組件中沒(méi)有插入任何代碼的話就會(huì)顯示組件插槽中的內(nèi)容
slot 代表父組件往子組件中 插入的標(biāo)簽
這里就代表組件子組件中的
<p>Dell</p> <child> <p>Dell</p> </child>
這里如果是這樣的
<child> </child>
就會(huì)顯示 <slot>默認(rèn)內(nèi)容</slot>中的默認(rèn)內(nèi)容
二、聚類插槽
1、如果不在子組件中使用插槽(slot),那么在子組件中寫(xiě)任何代碼都是無(wú)效的的,不會(huì)顯示
2、(插槽默認(rèn)值)如果子組件中沒(méi)有插入任何代碼的話就會(huì)顯示組件插槽中的內(nèi)容
這里如果是這樣的
<child> </child>
就會(huì)顯示<slot>默認(rèn)內(nèi)容</slot>中的 默認(rèn)內(nèi)容
3、聚類插槽
子組件這么寫(xiě):
template:`<div> <slot>默認(rèn)內(nèi)容</slot> <p>content</p> <slot>默認(rèn)內(nèi)容</slot> </div>
然后這么引用:
<child> <div>header</div> <div>footer</div> </child>
就會(huì)發(fā)現(xiàn)結(jié)果是
header
footer
content
header
footer
這個(gè)不是我的本意,那么怎么辦,這里就引入了聚類插槽
子組件:
template:`<div> <slot name='header'>默認(rèn)內(nèi)容</slot> <p>content</p> <slot name='footer'>默認(rèn)內(nèi)容</slot> </div>`
子組件引用:
<child> <div slot='header'>header</div> <div slot='footer'>footer</div> </child>
不難發(fā)現(xiàn)給每個(gè)想要指定的子組件插槽添加 name屬性,然后在引用中 slot中明確 是哪個(gè)即可也可以理解為引用中是用了兩個(gè)插槽同時(shí),默認(rèn)內(nèi)容同時(shí)適用在每個(gè)插槽
三、作用域插槽
這個(gè)是普通插槽的Demo
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Vue中使用插槽(slot)</title> <script src="https://cdn.jsdelivr.net/npm/vue@2.5.17/dist/vue.js"></script> </head> <body> <div id="root"> <!-- 1、如果不在子組件中使用插槽(slot),那么在子組件中寫(xiě)任何代碼都是無(wú)效的的,不會(huì)顯示 2、(插槽默認(rèn)值)如果子組件中沒(méi)有插入任何代碼的話就會(huì)顯示組件插槽中的內(nèi)容 這里如果是這樣的 <child> </child> 就會(huì)顯示 <slot>默認(rèn)內(nèi)容</slot>中的 默認(rèn)內(nèi)容 --> <child> <p>Dell</p> </child> </div> <script type="text/javascript"> Vue.component('child',{ /* slot 代表 父組件往子組件中 插入的標(biāo)簽 這里就代表 組件子組件中的 <p>Dell</p> <child> <p>Dell</p> </child> */ template:`<div> <slot>默認(rèn)內(nèi)容</slot> </div>` }); var vm = new Vue({ el:'#root', }); </script> </body> </html>
這個(gè)是聚類插槽的Demo
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Vue中使用插槽(slot)</title> <script src="https://cdn.jsdelivr.net/npm/vue@2.5.17/dist/vue.js"></script> </head> <body> <div id="root"> <!-- 1、如果不在子組件中使用插槽(slot),那么在子組件中寫(xiě)任何代碼都是無(wú)效的的,不會(huì)顯示 2、(插槽默認(rèn)值)如果子組件中沒(méi)有插入任何代碼的話就會(huì)顯示組件插槽中的內(nèi)容 這里如果是這樣的 <child> </child> 就會(huì)顯示 <slot>默認(rèn)內(nèi)容</slot>中的 默認(rèn)內(nèi)容 3、聚類插槽 子組件這么寫(xiě): template:`<div> <slot>默認(rèn)內(nèi)容</slot> <p>content</p> <slot>默認(rèn)內(nèi)容</slot> </div>` 然后這么引用: <child> <div>header</div> <div>footer</div> </child> 就會(huì)發(fā)現(xiàn)結(jié)果是 header footer content header footer 這個(gè)不是我的本意,那么怎么辦,這里就引入了聚類插槽 子組件: template:`<div> <slot name='header'>默認(rèn)內(nèi)容</slot> <p>content</p> <slot name='footer'>默認(rèn)內(nèi)容</slot> </div>` 子組件引用: <child> <div slot='header'>header</div> <div slot='footer'>footer</div> </child> 不難發(fā)現(xiàn)給每個(gè)想要指定的子組件插槽添加 name屬性, 然后在引用中 slot中明確 是哪個(gè)即可 也可以理解為引用中是用了兩個(gè)插槽 同時(shí),默認(rèn)內(nèi)容同時(shí)適用在每個(gè)插槽 --> <child> <div slot='header'>default header</div> <div slot='footer'>default footer</div> </child> </div> <script type="text/javascript"> Vue.component('child',{ /* slot 代表 父組件往子組件中 插入的標(biāo)簽 這里就代表 組件子組件中的 <p>Dell</p> <child> <p>Dell</p> </child> */ template:`<div> <slot name='header'>默認(rèn)內(nèi)容</slot> <p>content</p> <slot name='footer'>默認(rèn)內(nèi)容</slot> </div>` }); var vm = new Vue({ el:'#root', }); </script> </body> </html>
以上所述是小編給大家介紹的Vue中使用插槽(slot)、聚類插槽詳解整合,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
Vue3?實(shí)現(xiàn)一個(gè)自定義toast?小彈窗功能
這篇文章主要介紹了Vue3?實(shí)現(xiàn)一個(gè)自定義toast?小彈窗,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-09-09vue用vis插件如何實(shí)現(xiàn)網(wǎng)絡(luò)拓?fù)鋱D
這篇文章主要介紹了vue用vis插件如何實(shí)現(xiàn)網(wǎng)絡(luò)拓?fù)鋱D,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10Vue3項(xiàng)目中引入ElementUI并使用的示例詳解
ElementUI是一個(gè)強(qiáng)大的PC端UI組件框架,它不依賴于vue,但是卻是當(dāng)前和vue配合做項(xiàng)目開(kāi)發(fā)的一個(gè)比較好的ui框架,本文主要介紹了如何在vue3中引入使用ElementUI,需要的可以參考一下2023-06-06vue實(shí)現(xiàn)可移動(dòng)的懸浮按鈕
這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)可移動(dòng)的懸浮按鈕,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-03-03何時(shí)/使用 Vue3 render 函數(shù)的教程詳解
這篇文章主要介紹了何時(shí)/使用 Vue3 render 函數(shù)的教程詳解,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-07-07