vue2中插槽(slot)的基本使用規(guī)范
前言
在vue的開發(fā)過程中,我們會經(jīng)常使用到vue的slot插槽組件,vue官方文檔的描述:
Vue 實現(xiàn)了一套內(nèi)容分發(fā)的 API,這套 API 的設(shè)計靈感源自 Web Components 規(guī)范草案,將元素作為承載分發(fā)內(nèi)容的出口
slot大概分為以下幾種:
基礎(chǔ)slot組件(匿名插槽)
匿名插槽主要使用場景并不涉及特別復(fù)雜的業(yè)務(wù),更像是純展示組件內(nèi)容
<!--子組件--> <template> ?? ?<span> ?? ??? ?我是基礎(chǔ)slot子組件, 父組件傳過來的值: ?? ??? ?<span style="color: red"><slot></slot></span> ?? ?</span> </template>
<!--父組件--> <li> ? ? 基礎(chǔ)slot組件(匿名插槽):<Base>這是一段父組件傳過來的文字</Base> </li> import Base from "./Base.vue";
具名插槽
具名插槽,需要在父組件和子組件約定插槽名稱
<!--子組件--> <template> ?? ?<span> ?? ??? ?<span style="color: red"> ?? ??? ??? ?<slot name="name1"></slot> ?? ??? ??? ?<slot name="name2"></slot> ?? ??? ?</span> ?? ?</span> </template>
<!--父組件--> <li> ? ? <p>具名插槽:</p> ? ? <Specific> ? ? ?? ?<template v-slot:name1> ? ? ?? ??? ?<p>name1傳過來的內(nèi)容</p> ? ? ?? ?</template> ? ? ?? ?<template v-slot:name2> ? ? ?? ??? ?<p>name2傳過來的內(nèi)容</p> ? ? ?? ?</template> ? ? </Specific> </li> import Specific from "./Specific.vue";
作用域插槽
作用域插槽,子組件提供數(shù)據(jù),父組件接收子組件的值并展示和處理邏輯
<!--子組件-->
<template>
?? ?<span>
?? ??? ?<span>
?? ??? ??? ?<slot name="scopeName" v-bind:scopeData="age"></slot>
?? ??? ?</span>
?? ?</span>
</template>
<script lang="ts">
import { Component, Vue, Prop } from "vue-property-decorator";
@Component
export default class Scope extends Vue {
?? ?private age: Number = 23;
}
</script><!--父組件-->
<li>
?? ?<p>作用域插槽</p>
?? ?<Scope>
?? ??? ?<template v-slot:scopeName="childData">
?? ??? ??? ?作用域子組件slot返回的數(shù)據(jù):
?? ??? ??? ?<span style="color: red">
?? ??? ??? ??? ?{{ childData.scopeData }}
?? ??? ??? ?</span>
?? ??? ?</template>
?? ?</Scope>
</li>
import Specific from "./Specific.vue";解構(gòu)插槽
解構(gòu)插槽,類似在js書寫對象過程中的對象解構(gòu)
{ data:{ username:1 } }
<!--子組件-->
<template>
?? ?<span>
?? ??? ?<p>
?? ??? ??? ?<slot v-bind:user="user"></slot>
?? ??? ?</p>
?? ?</span>
</template>
<script lang="ts">
import { Component, Vue, Prop } from "vue-property-decorator";
@Component
export default class Deconstru extends Vue {
?? ?private user: Object = {
?? ??? ?name: "zhangsan",
?? ??? ?age: 23,
?? ?};
}
</script><!--父組件-->
<li>
?? ?<p>解構(gòu)插槽</p>
?? ?<Deconstru>
?? ??? ?<template v-slot="{ user: person }">
?? ??? ??? ?父組件模板:{{ person.name }},{{ person.age }}
?? ??? ?</template>
?? ?</Deconstru>
</li>
import Specific from "./Deconstru.vue";以上例子均已上傳至開源倉庫,后續(xù)關(guān)于vue的學(xué)習(xí)筆記均會更在在該項目上,歡迎star
碼云 https://gitee.com/lewyon/vue-note
githup https://github.com/akari16/vue-note
總結(jié)
到此這篇關(guān)于vue2中插槽(slot)的基本使用規(guī)范的文章就介紹到這了,更多相關(guān)vue2中slot使用規(guī)范內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue中el-date-picker選擇日期的限制的項目實踐
ElementUI的el-date-picker使用時,有時候想要限制用戶選擇的時間范圍,本文就來介紹了vue中el-date-picker選擇日期的限制的項目實踐,感興趣的可以了解一下2023-10-10
vue如何解決數(shù)據(jù)加載時,插值表達(dá)式閃爍問題
這篇文章主要介紹了vue如何解決數(shù)據(jù)加載時,插值表達(dá)式閃爍問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-01-01
Vue?2源碼解析ParseHTML函數(shù)HTML模板
這篇文章主要為大家介紹了Vue?2源碼解析ParseHTML函數(shù)HTML模板詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08
vue實現(xiàn)用戶長時間不操作自動退出登錄功能的實現(xiàn)代碼
這篇文章主要介紹了vue實現(xiàn)用戶長時間不操作自動退出登錄功能的實現(xiàn)代碼,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-07-07
vite5+vue3+?import.meta.glob動態(tài)導(dǎo)入vue組件圖文教程
import.meta.glob是Vite提供的一個特殊功能,它允許你在模塊范圍內(nèi)動態(tài)地導(dǎo)入多個模塊,這篇文章主要給大家介紹了關(guān)于vite5+vue3+?import.meta.glob動態(tài)導(dǎo)入vue組件的相關(guān)資料,需要的朋友可以參考下2024-07-07
vue 如何從單頁應(yīng)用改造成多頁應(yīng)用
這篇文章主要介紹了vue 如何從單頁應(yīng)用改造成多頁應(yīng)用,幫助大家更好的理解和使用vue,感興趣的朋友可以了解下2020-10-10

