Vue3中插槽(slot)用法匯總(推薦)
Vue中的插槽相信使用過(guò)Vue的小伙伴或多或少的都用過(guò),但是你是否了解它全部用法呢?本篇文章就為大家?guī)?lái)Vue3中插槽的全部用法來(lái)幫助大家查漏補(bǔ)缺。
什么是插槽
簡(jiǎn)單來(lái)說(shuō)就是子組件中的提供給父組件使用的一個(gè) 坑位 ,用 <slot></slot> 表示,父組件可以在這個(gè)坑位中填充任何模板代碼然后子組件中 <slot></slot> 就會(huì)被替換成這些內(nèi)容。比如一個(gè)最簡(jiǎn)單插槽例子
//父組件
<template>
<div>
<Child>Hello Juejin</Child>
</div>
</template>
<script setup lang="ts">
import Child from './Child.vue'
</script>
//子組件Child
<template>
<div>
<p>1</p>
<slot />
<p>2</p>
</div>
</template>子組件中的 <slot /> 便是父組件放在子組件標(biāo)簽 <Child> 之間的內(nèi)容。當(dāng)然這之間你可以傳入任何代碼片段,都會(huì)被放到 <slot /> 這個(gè)位置。

同樣的你也可以在標(biāo)簽 <Child> 之間放入變量,比如
//父組件
<template>
<div>
<Child>{{ msg }}</Child>
</div>
</template>
<script setup lang="ts">
import { ref } from 'vue'
import Child from './Child.vue'
const msg = ref('Hello Juejin')
</script>先解釋一下后面頻繁出現(xiàn)的兩個(gè)詞 插槽 和 插槽內(nèi)容 ,防止后面閱讀搞混了:

同樣的 插槽 表示的就是這個(gè) msg 變量。所以子組件 插槽 是可以訪問(wèn)到父組件的數(shù)據(jù)作用域,而 插槽內(nèi)容 是無(wú)法訪問(wèn)子組件的數(shù)據(jù)(即父組件中兩個(gè) <Child> 之間是不能使用子組件中的數(shù)據(jù)的),這就是所謂的渲染作用域。后面會(huì)介紹 插槽 向 插槽內(nèi)容 傳參的方式
默認(rèn)內(nèi)容
在父組件沒有提供任何 插槽內(nèi)容 的時(shí)候,我們是可以為子組件的 插槽 指定默認(rèn)內(nèi)容的,比如
//子組件
<template>
<div>
<slot>我是默認(rèn)內(nèi)容</slot>
</div>
</template>
//父組件1
<template>
<div>
<Child></Child>
</div>
</template>
<script setup>
import Child from './Child.vue'
</script>
//父組件2
<template>
<div>
<Child>Hello Juejin</Child>
</div>
</template>
<script setup>
import Child from './Child.vue'
</script>此時(shí) 父組件1 展示默認(rèn)內(nèi)容

父組件2 展示提供的內(nèi)容

具名插槽
很多時(shí)候一個(gè) 插槽 滿足不了我們的需求,我們需要多個(gè) 插槽 。于是就有了 具名插槽 ,就是具有名字的 插槽 。簡(jiǎn)單來(lái)說(shuō)這個(gè) 具名插槽 的目的就是讓一個(gè)蘿卜一個(gè)坑,讓它們呆在該呆的位置去。比如帶 name 的插槽 <slot name="xx"> 被稱為具名插槽。沒有提供 name 的 <slot> 會(huì)隱式地命名為“default”。在父組件中可以使用 v-slot:xxx (可簡(jiǎn)寫為 #xxx ) 指令的 <template> 元素將目標(biāo)插槽的名字傳下去匹配對(duì)應(yīng) 插槽 。比如
//子組件
<template>
<div>
<!-- 大蘿卜 -->
<div>
<slot name="bigTurnip"></slot>
</div>
<!-- 小蘿卜 -->
<div>
<slot name="smallTurnip"></slot>
</div>
<!-- 中蘿卜 -->
<div>
<slot name="midTurnip"></slot>
</div>
</div>
</template>
//父組件
<template>
<div>
<Child>
<!-- #smallTurnip 為v-slot:smallTurnip縮寫 -->
<template #smallTurnip>
小蘿卜
</template>
<template #midTurnip>
中蘿卜
</template>
<template #bigTurnip>
大蘿卜
</template>
</Child>
</div>
</template>
<script setup>
import Child from './Child.vue'
</script>
所以父組件中無(wú)需在意順序,只需要寫好模板命好名,它就會(huì)自動(dòng)去到它所對(duì)應(yīng)的位置。
動(dòng)態(tài)插槽名
動(dòng)態(tài)插槽名就是插槽名變成了變量的形式,我們可以隨時(shí)修改這個(gè)變量從而展示不同的效果。它的寫法是 v-slot:[變量名] 或者縮寫為 #[變量名] 。
//父組件
<template>
<div>
<Child>
<!-- 等同于#smallTurnip -->
<template #[slotName]>
小蘿卜
</template>
<template #midTurnip>
中蘿卜
</template>
<template #bigTurnip>
大蘿卜
</template>
</Child>
</div>
</template>
<script setup>
import { ref } from 'vue'
import Child from './Child.vue'
const slotName = ref('smallTurnip')
</script>作用域插槽
作用域插槽
上面說(shuō)過(guò) 插槽內(nèi)容 是無(wú)法訪問(wèn)子組件的數(shù)據(jù)的,但是如果我們想在 插槽內(nèi)容 訪問(wèn)子組件的狀態(tài)該怎么辦呢?
其實(shí) 插槽 可以像對(duì)組件傳遞 props 那樣,在 slot 標(biāo)簽綁定屬性從而傳遞給父組件中的 插槽內(nèi)容 。首先來(lái)看下默認(rèn)插槽的傳值方式
//子組件
<template>
<div>
<slot personName="xiaoyue" age="18"></slot>
</div>
</template>
//父組件
<template>
<div>
<Child v-slot="slotProps">
My name is {{ slotProps.personName }} and I am {{ slotProps.age }} years old this year
</Child>
</div>
</template>
<script setup>
import Child from './Child.vue'
</script>你還可以以結(jié)構(gòu)的形式獲取 slot 提供的數(shù)據(jù)
<template>
<div>
<Child v-slot="{ personName, age }">
My name is {{ personName }} and I am {{ age }} years old this year
</Child>
</div>
</template>
注意不能綁定 name 屬性,因?yàn)槟憬壎?nbsp;name 它就成了具名插槽了。同樣具名插槽中的 name 屬性也不會(huì)傳遞給 插槽內(nèi)容 。因?yàn)閭鬟f的參數(shù)只能在 插槽內(nèi)容 中使用,所以這類能夠接受參數(shù)的插槽就被稱為了 作用域插槽 。
具名作用域插槽
下面再看下 具名作用域插槽 它的傳參方式。它接收參數(shù)的方式是通過(guò) template 標(biāo)簽的指令 v-slot 的值獲取的,所以可以縮寫成這樣
//父組件
<template>
<div>
<Child>
<template #bigTurnip="bigTurnipProps">
{{ bigTurnipProps.message }}
</template>
</Child>
</div>
</template>
<script setup>
import Child from './Child.vue'
</script>
//子組件Child.vue
<template>
<div>
<!-- 大蘿卜 -->
<div>
<slot name="bigTurnip" message="我是蘿北"></slot>
</div>
</div>
</template>
這類插槽便是 具名作用域插槽 啦
寫在最后
到這里 插槽 (slot)的全部用法基本就已經(jīng)介紹完啦。
到此這篇關(guān)于Vue3中插槽(slot)用法匯總的文章就介紹到這了,更多相關(guān)Vue3 插槽slot內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue+springboot實(shí)現(xiàn)圖形驗(yàn)證碼Kaptcha的示例
圖形驗(yàn)證碼是做網(wǎng)站常用的功能,本文主要介紹了vue+springboot實(shí)現(xiàn)圖形驗(yàn)證碼Kaptcha的示例,具有一定的參考價(jià)值,感興趣的可以了解一下2023-11-11
解決Ant Design Modal內(nèi)嵌Form表單initialValue值不動(dòng)態(tài)更新問(wèn)題
這篇文章主要介紹了解決Ant Design Modal內(nèi)嵌Form表單initialValue值不動(dòng)態(tài)更新問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-10-10
vue?退出登錄?清除localStorage的問(wèn)題
這篇文章主要介紹了vue?退出登錄?清除localStorage的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-12-12
vue3項(xiàng)目如何使用prettier格式化代碼
這篇文章主要介紹了vue3項(xiàng)目如何使用prettier格式化代碼問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-10-10
Vue中this.$refs獲取為undefined的原因和解決辦法(this.$refs.屬性為undefined原因
在Vue項(xiàng)目開發(fā)中,使用this.$refs訪問(wèn)組件或DOM元素的引用時(shí),可能會(huì)遇到獲取為undefined的情況,這篇文章主要介紹了Vue中this.$refs獲取為undefined的原因和解決辦法,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-11-11
Vue.js實(shí)現(xiàn)微信過(guò)渡動(dòng)畫左右切換效果
這篇文章主要給大家介紹了利用Vue.js仿微信過(guò)渡動(dòng)畫左右切換效果的相關(guān)資料,需要用到的技術(shù)棧是Vue+Vuex。文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家具一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起看看吧。2017-06-06
Vue3中Element Plus Table(表格)點(diǎn)擊獲取對(duì)應(yīng)id方式
這篇文章主要介紹了Vue3中Element Plus Table(表格)點(diǎn)擊獲取對(duì)應(yīng)id方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-10-10

