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>
<!-- #簡寫-->
<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)文章
vue項(xiàng)目環(huán)境搭建?啟動(dòng)?移植操作示例及目錄結(jié)構(gòu)分析
這篇文章主要介紹了vue項(xiàng)目環(huán)境搭建、啟動(dòng)、項(xiàng)目移植、項(xiàng)目目錄結(jié)構(gòu)分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步早日升職加薪2022-04-04
Vue3+Hooks實(shí)現(xiàn)4位隨機(jī)數(shù)和60秒倒計(jì)時(shí)的示例代碼
Vue3的Hooks是一種新的 API,本文主要介紹了Vue3+Hooks實(shí)現(xiàn)4位隨機(jī)數(shù)和60秒倒計(jì)時(shí)的示例代碼,具有一定的參考價(jià)值,感興趣的可以了解一下2024-04-04
應(yīng)用provide與inject刷新Vue頁面方法
這篇文章主要介紹了應(yīng)用provide與inject刷新Vue頁面的兩種方法,有需要的朋友可以借鑒參考下,希望能夠有所幫助,多多進(jìn)步,祝大家早日升職加薪2021-09-09
reactive readonly嵌套對(duì)象轉(zhuǎn)換功能實(shí)現(xiàn)詳解
這篇文章主要為大家介紹了reactive readonly嵌套對(duì)象轉(zhuǎn)換功能實(shí)現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12
vue3實(shí)戰(zhàn)教程之a(chǎn)xios的封裝和環(huán)境變量
這篇文章主要給大家介紹了關(guān)于vue3實(shí)戰(zhàn)教程之a(chǎn)xios的封裝和環(huán)境變量的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2022-02-02

