Vue中slot插槽作用與原理詳解
1、作用
- 父組件向子組件傳遞內(nèi)容
- 擴(kuò)展、復(fù)用、定制組件
2、插槽內(nèi)心
2.1、默認(rèn)插槽
把父組件中的數(shù)組,顯示在子組件中,子組件通過一個(gè)slot插槽標(biāo)簽顯示父組件中的數(shù)據(jù)。
子組件
<template>
<div class="slotChild">
<h4>{{msg}}</h4>
<slot>這是子組件插槽默認(rèn)的值</slot>
</div>
</template>
<script>
export default {
name: "slotChild",
data() {
return {
msg: "vue中的插槽---子組件"
}
}
}
</script>父組件
<template>
<div class="slotStudy">
<h1>{{ msg }}</h1>
<SlotChild>
<p>這是父組件傳入的值,將會(huì)替換子組件中slot中編寫的默認(rèn)值</p>
</SlotChild>
</div>
</template>
<script>
import SlotChild from "@/components/slot/SlotChild";
export default {
name: "slotStudy",
components: {SlotChild},
data() {
return {
msg: "vue中的插槽---父組件"
}
}
}
</script>
<SlotChild></SlotChild>

2.2、具名插槽(命名插槽)
父組件中通過slot屬性,給插槽命名,在子組件中通過slot標(biāo)簽,根據(jù)定義好的名字填充到對(duì)應(yīng)的位置。這樣就可以指定多個(gè)可區(qū)分的slot,在使用組件時(shí)靈活的進(jìn)行插值。
子組件:
<template>
<div class="slotChild">
<h4>{{msg}}</h4>
<h1><slot name="h_1"></slot></h1>
<h2><slot name="h_2"></slot></h2>
<h3><slot name="h_3"></slot></h3>
</div>
</template>
<script>
export default {
name: "slotChild",
data() {
return {
msg: "vue中的插槽---子組件"
}
}
}
</script>父組件:
<template>
<div class="slotStudy">
<h1>{{ msg }}</h1>
<SlotChild>
<template v-slot:h_1>h1111111111的內(nèi)容</template>
<!-- #簡(jiǎn)寫-->
<template #h_2>h2222222的內(nèi)容</template>
<template v-slot:h_3>h33333的內(nèi)容</template>
</SlotChild>
</div>
</template>
<script>
import SlotChild from "@/components/slot/SlotChild";
export default {
name: "slotStudy",
components: {SlotChild},
data() {
return {
msg: "vue中的插槽---父組件"
}
}
}
</script>
2.3、作用域插槽
用得不多。
將子組件中data的數(shù)據(jù)傳出,在父組件中使用。子組件渲染作用域插槽時(shí),可以將子組件內(nèi)部的數(shù)據(jù)傳遞給父組件,讓父組件根據(jù)子組件的傳遞過來的數(shù)據(jù)決定如何渲染該插槽。在標(biāo)簽中通過v-slot="要傳過來的數(shù)據(jù)"來接收數(shù)據(jù)。
實(shí)現(xiàn)原理
實(shí)現(xiàn)原理:當(dāng)子組件vm實(shí)例化時(shí),獲取到父組件傳入的slot標(biāo)簽的內(nèi)容,存放在vm. s l o t 中,默認(rèn)插槽為 v m . slot中,默認(rèn)插槽為vm. slot中,默認(rèn)插槽為vm.slot.default,具名插槽為vm. s l o t . x x x , x x x 為插槽名,當(dāng)組件執(zhí)行渲染函數(shù)時(shí)候,遇到 s l o t 標(biāo)簽,使用 slot.xxx,xxx 為插槽名,當(dāng)組件執(zhí)行渲染函數(shù)時(shí)候,遇到slot標(biāo)簽,使用 slot.xxx,xxx為插槽名,當(dāng)組件執(zhí)行渲染函數(shù)時(shí)候,遇到slot標(biāo)簽,使用slot中的內(nèi)容進(jìn)行替換,此時(shí)可以為插槽傳遞數(shù)據(jù),若存在數(shù)據(jù),則可稱該插槽為作用域插槽。
子組件:
<template>
<div class="slotChild">
<h4>{{ msg }}</h4>
<h1>
<slot :str="strDate" name="n_str">{{ strDate.name }}</slot>
</h1>
<h2>
<slot :str="strDate" name="j_str">{{ strDate.job }}</slot>
</h2>
</div>
</template>
<script>
export default {
name: "slotChild",
data() {
return {
msg: "vue中的插槽---子組件",
strDate: {
name: "學(xué)習(xí)前端的小方同學(xué)",
job: "找工作中",
age:"我每年都是18"
}
}
}
}
</script>父組件:
<template>
<div class="slotStudy">
<h1>{{ msg }}</h1>
<SlotChild>
<template #n_str="strProps">
{{ strProps.str.job }}
</template>
<template v-slot:j_str="strProps">
{{ strProps.str.age }}
</template>
</SlotChild>
</div>
</template>
<script>
import SlotChild from "@/components/slot/SlotChild";
export default {
name: "slotStudy",
components: {SlotChild},
data() {
return {
msg: "vue中的插槽---父組件"
}
}
}
</script>
到此這篇關(guān)于Vue中slot插槽作用與原理詳解的文章就介紹到這了,更多相關(guān)Vue slot插槽內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
VsCode新建VueJs項(xiàng)目的詳細(xì)步驟
本篇文章主要介紹了VsCode新建VueJs項(xiàng)目的詳細(xì)步驟,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-09-09
vue 對(duì)象添加或刪除成員時(shí)無法實(shí)時(shí)更新的解決方法
這篇文章主要介紹了vue 對(duì)象添加或刪除成員時(shí)無法實(shí)時(shí)更新的解決方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-05-05
vue項(xiàng)目打包清除console.log的四種方法總結(jié)
大家在項(xiàng)目開發(fā)的時(shí)候,需要看看一些后端接口返回的結(jié)果,會(huì)多次使用console.log項(xiàng)目開發(fā)完成打包的時(shí)候,發(fā)現(xiàn)控制臺(tái)一堆的console.log,非常頭疼,下面這篇文章主要給大家介紹了關(guān)于vue項(xiàng)目打包清除console.log的四種方法,需要的朋友可以參考下2023-04-04
vue基于echarts實(shí)現(xiàn)立體柱形圖
這篇文章主要為大家詳細(xì)介紹了vue基于echarts實(shí)現(xiàn)立體柱形圖,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09
Elementui如何限制el-input框輸入小數(shù)點(diǎn)
這篇文章主要介紹了Elementui如何限制el-input框輸入小數(shù)點(diǎn),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08
vue組件之間通信實(shí)例總結(jié)(點(diǎn)贊功能)
這篇文章主要介紹了vue組件之間通信,結(jié)合實(shí)例形式分析了vue父子組件、兄弟組件之間通信的原理、實(shí)現(xiàn)方法,并給出了一個(gè)類似點(diǎn)贊功能的總結(jié)實(shí)例,需要的朋友可以參考下2018-12-12
在Vue中創(chuàng)建可重用的 Transition的方法
這篇文章主要介紹了在Vue中創(chuàng)建可重用的 Transition,本文通過示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-06-06
vue3在構(gòu)建時(shí)使用魔法糖語法時(shí)defineProps和defineEmits的注意事項(xiàng)小結(jié)
在 Vue 3.2+ 版本中,可以使用 <script setup> 替代傳統(tǒng)的 script標(biāo)簽來編寫組件,它提供了更簡(jiǎn)潔的語法來編寫 Composition API 代碼,這篇文章主要介紹了vue3在構(gòu)建時(shí)使用魔法糖語法時(shí)defineProps和defineEmits的注意事項(xiàng)小結(jié),需要的朋友可以參考下2024-04-04

