vue中的v-slot指令使用
定義
在Vue中, v-slot 指令用于定義插槽的模板內(nèi)容。它用于在父組件中傳遞內(nèi)容到子組件中的插槽。 v-slot 指令可以用于 標(biāo)簽或組件標(biāo)簽上,以便在子組件中使用插槽。
語法
使用 v-slot 指令時(shí),可以使用以下兩種語法:
1.縮寫語法: # 符號(hào)表示 v-slot 指令,后面跟插槽名稱。
<template #插槽名稱> <!-- 插槽內(nèi)容 --> </template>
2.完整語法: v-slot 指令后面跟著 : ,后面是插槽名稱。
<template v-slot:插槽名稱> <!-- 插槽內(nèi)容 --> </template>
使用場景
v-slot 指令的使用場景包括但不限于以下幾種:
- 在組件中使用插槽,將父組件中的內(nèi)容傳遞給子組件。
- 在子組件中使用具名插槽,根據(jù)插槽名稱渲染不同的內(nèi)容。
- 在子組件中使用作用域插槽,將子組件中的數(shù)據(jù)傳遞到父組件中進(jìn)行渲染。
場景一
在組件中使用插槽,將父組件中的內(nèi)容傳遞給子組件。
父組件
<template>
<div>
<child-component>
<template v-slot:default>
<!-- 插槽內(nèi)容 -->
<p>This is the content passed from the parent component.</p>
</template>
</child-component>
</div>
</template>子組件
<template>
<div>
<slot></slot>
</div>
</template>場景二
在子組件中使用具名插槽,根據(jù)插槽名稱渲染不同的內(nèi)容:
父組件
<template>
<div>
<child-component>
<template v-slot:header>
<!-- 插槽內(nèi)容 -->
<h1>Header Content</h1>
</template>
<template v-slot:body>
<!-- 插槽內(nèi)容 -->
<p>Body Content</p>
</template>
</child-component>
</div>
</template>子組件
<template>
<div>
<slot name="header"></slot>
<slot name="body"></slot>
</div>
</template>場景三
在子組件中使用作用域插槽,將子組件中的數(shù)據(jù)傳遞到父組件中進(jìn)行渲染:
父組件
<template>
<div>
<child-component>
<template v-slot:default="slotProps">
<!-- 插槽內(nèi)容 -->
<p>{{ slotProps.message }}</p>
</template>
</child-component>
</div>
</template>子組件
<template>
<div>
<slot :message="message"></slot>
</div>
</template>
<script>
export default {
data() {
return {
message: "Hello from child component!"
};
}
};
</script>在router-view中的應(yīng)用,拿到router-view中的Component值,同時(shí)利用component 標(biāo)簽動(dòng)態(tài)渲染組件
<router-view v-slot="{ Component, route }">
<transition appear name="fade-transform" mode="out-in">
<keep-alive :include="keepAliveName">
<component :is="Component" v-if="isRouterShow" :key="route.fullPath" />
</keep-alive>
</transition>
</router-view>tips
如果父組件沒有向插槽傳入值,則子組件會(huì)顯示原來的內(nèi)容,當(dāng)傳入具體的值時(shí),則會(huì)覆蓋掉插槽內(nèi)的內(nèi)容
子組件:
<template> <slot name="a1" :content="slot_data"> <h1>child-123</h1> </slot> </template> <script lang="ts" setup> const slot_data = "child-content"; </script> <style scoped></style>
父組件:
<template>
<div>
<h5>slot-test</h5>
<child>
<!-- <template #a1="{ content }">
<div>{{ content }}</div>
</template> -->
</child>
</div>
</template>
<script lang="ts" setup>
import child from "./child.vue";
</script>
<style scoped></style>此時(shí)注釋掉插值代碼,結(jié)果如圖,只會(huì)顯示原來槽內(nèi)內(nèi)容

父組件代碼修改如下
<template>
<div>
<h5>slot-test</h5>
<child>
<template #a1="{ content }">
<div>{{ content }} 我是父組件</div>
</template>
</child>
</div>
</template>
<script lang="ts" setup>
import child from "./child.vue";
</script>
<style scoped></style>顯示內(nèi)容如圖所示,則會(huì)覆蓋掉原來槽值

在v-slot中,既可以由子組件向父組件傳值(slot_data),又可以由父組件向子組件傳遞html內(nèi)容,可以看做是‘’雙向的‘’在一些場景比如子組件渲染的內(nèi)容既需要子組件數(shù)據(jù)又需要父組件數(shù)據(jù)時(shí)可以考慮使用插槽來完成
props同樣也可以向子組件傳值,在子組件中同一渲染完成,這是之前一直使用的方式,之后可以考慮使用插槽,拿到子組件中的值,又可以向子組件傳遞內(nèi)容
只有一個(gè)默認(rèn)插槽時(shí)
可以直接這樣寫,類似于上述router-view的用法子組件:
<template> <slot :content="slot_data" :content2="slot_data2"> </slot> </template> <script lang="ts" setup> const slot_data = "child-content"; const slot_data2 = "child-content2"; </script>
父組件:content,content2采用解構(gòu)賦值直接從slotProps值(默認(rèn)傳遞變量的名稱)中得到,templete也可以省略,child標(biāo)簽內(nèi)的所有值都會(huì)被傳入插槽
<template>
<div>
<h5>slot-test</h5>
<child v-slot="{ content, content2 }">
<!-- <h1>{{ content }}</h1> -->
{{ content }}
{{ content2 }}
784561
</child>
</div>
</template>
<script lang="ts" setup>
import child from "./child.vue";
</script>結(jié)果如圖:

到此這篇關(guān)于vue v-slot指令的文章就介紹到這了,更多相關(guān)vue v-slot指令內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vite打包優(yōu)化CDN壓縮的分析實(shí)現(xiàn)
我們?cè)谌粘5墓ぷ髦锌隙〞?huì)遇到項(xiàng)目打包優(yōu)化等問題,本文主要介紹了vite打包優(yōu)化CDN壓縮的分析實(shí)現(xiàn),具有一定的參加價(jià)值,感興趣的可以了解一下2024-07-07
Vue實(shí)現(xiàn)未登錄跳轉(zhuǎn)到登錄頁的示例代碼
本文主要介紹了Vue實(shí)現(xiàn)未登錄跳轉(zhuǎn)到登錄頁的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08
vue中添加語音播報(bào)功能的實(shí)現(xiàn)
本文主要介紹了vue中添加語音播報(bào)功能的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04
基于vue.js實(shí)現(xiàn)無縫滾動(dòng)的兩種方法
在現(xiàn)代的網(wǎng)頁設(shè)計(jì)中,無縫滾動(dòng)廣告特效已經(jīng)變得非常流行,這種特效能夠吸引用戶的注意力,同時(shí)展示多個(gè)廣告內(nèi)容,這篇文章主要介紹了基于vue.js實(shí)現(xiàn)無縫滾動(dòng)的兩種方法,需要的朋友可以參考下2026-02-02
vue中ts無法識(shí)別引入的vue文件,提示找不到xxx.vue模塊的解決
這篇文章主要介紹了vue中ts無法識(shí)別引入的vue文件,提示找不到xxx.vue模塊的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-09-09
Vue——解決報(bào)錯(cuò) Computed property "****" was assigned to but it ha
這篇文章主要介紹了Vue——解決報(bào)錯(cuò) Computed property "****" was assigned to but it has no setter.的方法,幫助大家更好的理解和使用vue框架,感興趣的朋友可以了解下2020-12-12
vite(vue3)配置內(nèi)網(wǎng)ip訪問的方法步驟
Vite是一個(gè)快速的構(gòu)建工具,Vue3是一個(gè)流行的JavaScript框架,下面這篇文章主要給大家介紹了關(guān)于vite(vue3)配置內(nèi)網(wǎng)ip訪問的方法步驟,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下2023-05-05
Vue按回車鍵進(jìn)行搜索的實(shí)現(xiàn)方式
這篇文章主要介紹了Vue按回車鍵進(jìn)行搜索的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-01-01
Vue3+Vite項(xiàng)目按需自動(dòng)導(dǎo)入配置以及一些常見問題修復(fù)
Vite是一種新型前端構(gòu)建工具,能夠顯著提升前端開發(fā)體驗(yàn),下面這篇文章主要給大家介紹了關(guān)于Vue3+Vite項(xiàng)目按需自動(dòng)導(dǎo)入配置以及一些常見問題修復(fù)的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-02-02

