vue中的v-slot指令使用
定義
在Vue中, v-slot 指令用于定義插槽的模板內(nèi)容。它用于在父組件中傳遞內(nèi)容到子組件中的插槽。 v-slot 指令可以用于 標簽或組件標簽上,以便在子組件中使用插槽。
語法
使用 v-slot 指令時,可以使用以下兩種語法:
1.縮寫語法: # 符號表示 v-slot 指令,后面跟插槽名稱。
<template #插槽名稱> <!-- 插槽內(nèi)容 --> </template>
2.完整語法: v-slot 指令后面跟著 : ,后面是插槽名稱。
<template v-slot:插槽名稱> <!-- 插槽內(nèi)容 --> </template>
使用場景
v-slot 指令的使用場景包括但不限于以下幾種:
- 在組件中使用插槽,將父組件中的內(nèi)容傳遞給子組件。
- 在子組件中使用具名插槽,根據(jù)插槽名稱渲染不同的內(nèi)容。
- 在子組件中使用作用域插槽,將子組件中的數(shù)據(jù)傳遞到父組件中進行渲染。
場景一
在組件中使用插槽,將父組件中的內(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ù)傳遞到父組件中進行渲染:
父組件
<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中的應用,拿到router-view中的Component值,同時利用component 標簽動態(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
如果父組件沒有向插槽傳入值,則子組件會顯示原來的內(nèi)容,當傳入具體的值時,則會覆蓋掉插槽內(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>
此時注釋掉插值代碼,結(jié)果如圖,只會顯示原來槽內(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)容如圖所示,則會覆蓋掉原來槽值
在v-slot中,既可以由子組件向父組件傳值(slot_data),又可以由父組件向子組件傳遞html內(nèi)容,可以看做是‘’雙向的‘’在一些場景比如子組件渲染的內(nèi)容既需要子組件數(shù)據(jù)又需要父組件數(shù)據(jù)時可以考慮使用插槽來完成
props同樣也可以向子組件傳值,在子組件中同一渲染完成,這是之前一直使用的方式,之后可以考慮使用插槽,拿到子組件中的值,又可以向子組件傳遞內(nèi)容
只有一個默認插槽時
可以直接這樣寫,類似于上述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值(默認傳遞變量的名稱)中得到,templete也可以省略,child標簽內(nèi)的所有值都會被傳入插槽
<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é)果如圖:
到此這篇關于vue v-slot指令的文章就介紹到這了,更多相關vue v-slot指令內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
vue-cli3項目升級到vue-cli4 的方法總結(jié)
這篇文章主要介紹了vue-cli3項目升級到vue-cli4 的方法總結(jié),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-03-03el-table樹形數(shù)據(jù)量過大,導致頁面卡頓問題及解決
這篇文章主要介紹了el-table樹形數(shù)據(jù)量過大,導致頁面卡頓問題及解決,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-04-04詳解vue-cli項目中怎么使用mock數(shù)據(jù)
這篇文章主要介紹了vue-cli項目中怎么使用mock數(shù)據(jù),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-05-05element的表單校驗證件號規(guī)則及輸入“無”的情況校驗通過(示例代碼)
這篇文章主要介紹了element的表單校驗證件號規(guī)則及輸入“無”的情況校驗通過,使用方法對校驗數(shù)據(jù)進行過濾判斷,本文通過示例代碼給大家介紹的非常詳細,感興趣的朋友一起看看吧2023-11-11解決element-ui的el-select選擇器的@blur事件失效的坑
這篇文章主要介紹了解決element-ui的el-select選擇器的@blur事件失效的坑,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-09-09