vue中slot(插槽)的介紹與使用
什么是插槽?
插槽(Slot)是Vue提出來的一個(gè)概念,正如名字一樣,插槽用于決定將所攜帶的內(nèi)容,插入到指定的某個(gè)位置,從而使模板分塊,具有模塊化的特質(zhì)和更大的重用性。插槽顯不顯示、怎樣顯示是由父組件來控制的,而插槽在哪里顯示就由子組件來進(jìn)行控制
Vue slot 原理
在web-components中有slot的概念,https://developers.google.com/web/fundamentals/web-components/shadowdom。
<slot> 元素
Shadow DOM 使用 <slot> 元素將不同的 DOM 樹組合在一起。Slot 是組件內(nèi)部的占位符,用戶可以使用自己的標(biāo)記來填充。
通過定義一個(gè)或多個(gè) slot,您可將外部標(biāo)記引入到組件的 shadow DOM 中進(jìn)行渲染。 這相當(dāng)于您在說“在此處渲染用戶的標(biāo)記”。
注:Slot 是為網(wǎng)絡(luò)組件創(chuàng)建“聲明性 API”的一種方法。它們混入到用戶的 DOM 中,幫助對(duì)整個(gè)組件進(jìn)行渲染,從而將不同的 DOM 樹組合在一起。
怎么用插槽?
默認(rèn)插槽
父組件
<template> <div> 我是父組件 <slotOne1> <p style="color:red">我是父組件插槽內(nèi)容</p> </slotOne1> </div> </template>
在父組件引用的子組件中寫入想要顯示的內(nèi)容(可以使用標(biāo)簽,也可以不用)
子組件(slotOne1)
<template> <div class="slotOne1"> <div>我是slotOne1組件</div> <slot></slot> </div> </template>
在子組件中寫入slot,slot所在的位置就是父組件要顯示的內(nèi)容
當(dāng)然再父組件引用的子組件中也可以寫入其他組件
父組件
<template> <div> 我是父組件 <slotOne1> <p style="color:red">我是父組件插槽內(nèi)容</p> <slot-one2></slot-one2> </slotOne1> </div> </template>
子組件(slotOne2)
<template> <div class="slotOne2"> 我是slotOne2組件 </div> </template>
具名插槽
子組件
<template> <div class="slottwo"> <div>slottwo</div> <slot name="header"></slot> <slot></slot> <slot name="footer"></slot> </div> </template>
在子組件中定義了三個(gè)slot標(biāo)簽,其中有兩個(gè)分別添加了name屬性header和footer
父組件
<template> <div> 我是父組件 <slot-two> <p>啦啦啦,啦啦啦,我是賣報(bào)的小行家</p> <template slot="header"> <p>我是name為header的slot</p> </template> <p slot="footer">我是name為footer的slot</p> </slot-two> </div> </template>
在父組件中使用template并寫入對(duì)應(yīng)的slot值來指定該內(nèi)容在子組件中現(xiàn)實(shí)的位置(當(dāng)然也不用必須寫到template),沒有對(duì)應(yīng)值的其他內(nèi)容會(huì)被放到子組件中沒有添加name屬性的slot中
插槽的默認(rèn)內(nèi)容
父組件
<template> <div> 我是父組件 <slot-two></slot-two> </div> </template>
子組件
<template> <div class="slottwo"> <slot>我不是賣報(bào)的小行家</slot> </div> </template>
可以在子組件的slot標(biāo)簽中寫入內(nèi)容,當(dāng)父組件沒有寫入內(nèi)容時(shí)會(huì)顯示子組件的默認(rèn)內(nèi)容,當(dāng)父組件寫入內(nèi)容時(shí),會(huì)替換子組件的默認(rèn)內(nèi)容
編譯作用域
父組件
<template> <div> 我是父組件 <slot-two> <p>{{name}}</p> </slot-two> </div> </template> <script> export default { data () { return { name: 'Jack' } } } </script>
子組件
<template> <div class="slottwo"> <slot></slot> </div> </template>
作用域插槽
子組件
<template> <div> 我是作用域插槽的子組件 <slot :data="user"></slot> </div> </template> <script> export default { name: 'slotthree', data () { return { user: [ {name: 'Jack', sex: 'boy'}, {name: 'Jone', sex: 'girl'}, {name: 'Tom', sex: 'boy'} ] } } } </script>
在子組件的slot標(biāo)簽上綁定需要的值
父組件
<template> <div> 我是作用域插槽 <slot-three> <template slot-scope="user"> <div v-for="item in user.data" :key="item.id"> {{item}} </div> </template> </slot-three> </div> </template>
在父組件上使用slot-scope屬性,user.data就是子組件傳過來的值
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
相關(guān)文章
vue實(shí)現(xiàn)動(dòng)態(tài)控制表格列的顯示隱藏
這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)動(dòng)態(tài)控制表格列的顯示隱藏,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04vue項(xiàng)目展示pdf文件的方法實(shí)現(xiàn)
本文主要介紹了vue項(xiàng)目展示pdf文件的方法實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07Ant?Design_Form表單上傳文件組件實(shí)現(xiàn)詳解
這篇文章主要為大家介紹了Ant?Design_Form表單上傳文件組件實(shí)現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03