vue如何通過插槽組件之間數(shù)據(jù)傳遞
vue通過插槽組件之間數(shù)據(jù)傳遞
1.父向子傳值
//--->Father.vue <template> <div id="father"> <p>黃色為父組件---data的值為:{{ data }}</p> <Son> <template #usname> <p>data的值為:{{data}}</p>//將父組件的data數(shù)據(jù)傳入子組件的<slot>插槽 </template> </Son> </div> </template> <script> import Son from "./Son.vue"; export default { data() { return { data: "Father", }; }, components: { Son, }, }; </script> <style scoped> #father { width: 400px; height: 400px; background-color: yellow; } </style> //--->son.vue <template> <div id="son"> <p>藍(lán)色為子組件</p> <slot name="usname"></slot> </div> </template> <script> export default {}; </script> <style scoped> #son { width: 200px; height: 200px; background-color: skyblue; } </style>
2.子向父傳值
//--->Father.vue <template> <div id="father"> <p>黃色為父組件---data的值為:{{ data }}</p> <button @click="upp">1</button>//點擊查看傳過來的值 <Son> <template #usname="obj">//通過v-slot指令接收son.vue傳過來的值 <p>data的值為:{{ data }}</p> <p>son_data的值為{{ obj.son }}</p> <button @click="son_data = obj.son">1</button >//通過點擊按鈕將傳過來的值保存在Father組件的son_data數(shù)據(jù)中 </template> </Son> </div> </template> <script> import Son from "./Son.vue"; export default { data() { return { data: "Father", son_data: "", }; }, components: { Son, }, methods: { upp() { console.log(this.son_data); }, }, }; </script> <style scoped> #father { width: 400px; height: 400px; background-color: yellow; } </style> //--->Son.vue <template> <div id="son"> <p>藍(lán)色為子組件</p> <slot name="usname" :son='son_data'></slot>//添加自定義屬性son將son_data數(shù)據(jù)傳入父組件 </div> </template> <script> export default { data(){ return{ son_data:'Son' } } }; </script> <style scoped> #son { width: 200px; height: 200px; background-color: skyblue; } </style>
vue跨組件動態(tài)插槽傳遞
在看coderwhy老師后臺管理系統(tǒng)項目實戰(zhàn)視頻的時候發(fā)現(xiàn)組件封裝的思路與動態(tài)插槽傳遞非常有意思,記錄一下!
需求及基本信息說明
組件Goods調(diào)用組件page-content,組件page-content調(diào)用組件hy-table。
為了方便使用,組件page-content封裝公共插槽(如果將所有頁面的私有的插槽都一股腦放到組件page-content中封裝性會變差),需要在Goods中傳遞私有插槽內(nèi)容在hy-table中顯示。
這時候就產(chǎn)生了跨組件插槽傳遞的需求,而由于編譯作用域限制的原因,Goods組件中的#image不能直接被hy-table接收,而需要先由page-content接收goods中的數(shù)據(jù),再由page-content將數(shù)據(jù)傳遞到hy-table中。
而實際開發(fā)中不可能在page-content中將插槽名稱寫死,上面圖例page-content中只是簡單拿#image進(jìn)行舉例,實際開發(fā)中可以在組件page-content中動態(tài)插入私有插槽,實現(xiàn)如下。
代碼
- Goods.vue
Goods中使用組件page-content并傳入配置文件contentTableConfig,并向page-content中名字為image的插槽提供內(nèi)容<el-image>
<template> <div class="goods"> <page-content :contentTableConfig="contentTableConfig" pageName="goods"> <template #image="scope"> <el-image style="width: 60px; height: 60px" :src="scope.row.imgUrl" :preview-src-list="[scope.row.imgUrl]" > </el-image> </template> <template #oldPrice="scope">{{ '¥' + scope.row.oldPrice }}</template> </page-content> </div> </template>
- contentTableConfig配置文件數(shù)據(jù)
其中slotName為status、createAt、updateAt、handler為公共插槽配置
export const contentTableConfig = { title: '商品列表', propList: [ { prop: 'name', label: '商品名稱', minWidth: '80' }, { prop: 'oldPrice', label: '原價格', minWidth: '80', slotName: 'oldPrice' }, { prop: 'newPrice', label: '現(xiàn)價格', minWidth: '80' }, { prop: 'imgUrl', label: '商品圖片', minWidth: '100', slotName: 'image' }, { prop: 'status', label: '狀態(tài)', minWidth: '100', slotName: 'status' }, { prop: 'createAt', label: '創(chuàng)建時間', minWidth: '250', slotName: 'createAt' }, { prop: 'updateAt', label: '更新時間', minWidth: '250', slotName: 'updateAt' }, { label: '操作', minWidth: '120', slotName: 'handler' } ], showIndexColumn: true, showSelectColumn: true }
- page-content.vue
定義一個函數(shù)otherPropSlots對配置信息進(jìn)行過濾,去掉公共的插槽名稱,剩下就是私有的插槽名稱了,返回一個包含私有插槽的數(shù)組,在template中對這個數(shù)組進(jìn)行遍歷,slot :name="私有插槽名"接收Goods中的內(nèi)容,“#私有插槽名”的template向子組件hy-table傳遞內(nèi)容
<template> <div class="page-content"> <hy-table v-bind="contentTableConfig" > <template #status="scope"> <el-button plain size="mini" :type="scope.row.enable ? 'success' : 'danger'" > {{ scope.row.enable ? '啟用' : '禁用' }} </el-button> </template> <template #createAt="scope"> <span>{{ $filters.formatTime(scope.row.createAt) }}</span> </template> <template #updateAt="scope"> <span>{{ $filters.formatTime(scope.row.updateAt) }}</span> </template> <template #handler> <div class="handle-btns"> <el-button v-if="isUpdate" icon="el-icon-edit" size="mini" type="text" >編輯</el-button > <el-button v-if="isDelete" icon="el-icon-delete" size="mini" type="text" >刪除</el-button > </div> </template> <!-- 在page-content中動態(tài)插入剩余的插槽 --> <template v-for="item in otherPropSlots" :key="item.prop" #[item.slotName]="scope" > <template v-if="item.slotName"> <slot :name="item.slotName" :row="scope.row"></slot> </template> </template> </hy-table> </div> </template> <script lang="ts"> import { defineComponent } from 'vue' import HyTable from '@/base-ui/table' export default defineComponent({ components: { HyTable }, props: { contentTableConfig: { type: Object, require: true }, pageName: { type: String, required: true } }, setup(props) { // 獲取其他的動態(tài)插槽名稱 const otherPropSlots = props.contentTableConfig?.propList.filter( (item: any) => { if (item.slotName === 'status') return false if (item.slotName === 'createAt') return false if (item.slotName === 'updateAt') return false if (item.slotName === 'handler') return false return true } ) return { otherPropSlots } } }) </script>
想說的話:
動態(tài)傳遞插槽和封裝的思路絕了,需要花好多功夫和時間去消化~
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Vue選項之propsData傳遞數(shù)據(jù)方式
這篇文章主要介紹了Vue選項之propsData傳遞數(shù)據(jù)方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-10-10antd Form組件方法getFieldsValue獲取自定義組件的值操作
這篇文章主要介紹了antd Form組件方法getFieldsValue獲取自定義組件的值操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-10-10Vue3+Element-Plus?實現(xiàn)點擊左側(cè)菜單時顯示不同內(nèi)容組件展示在Main區(qū)域功能
這篇文章主要介紹了Vue3+Element-Plus?實現(xiàn)點擊左側(cè)菜單時顯示不同內(nèi)容組件展示在Main區(qū)域功能,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-01-01Vue+Openlayer使用modify修改要素的完整代碼
這篇文章主要介紹了Vue+Openlayer使用modify修改要素的完整代碼,代碼簡單易懂,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-09-09詳解Vue3中shallowRef和shallowReactive的使用
這篇文章主要為大家介紹了Vue3中shallowRef和shallowReactive函數(shù)的使用方法,文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)Vue有一定的幫助,需要的可以參考一下2022-07-07