Vue如何跨組件傳遞Slot的實現(xiàn)
在開發(fā)過程中遇到這樣一個問題,如何跨組件傳遞插槽。因為在開發(fā)類似樹組件的過程中,插槽需要通過外部傳遞到樹的根節(jié)點,然后通過根節(jié)點依次傳遞到各個葉子節(jié)點。那么如何把根節(jié)點的Slot如傳遞給子組件呢?
我們在開發(fā)過程中,希望可以這樣實現(xiàn)重新定義葉子節(jié)點的結(jié)構(gòu):
<data-tree>
<template v-slot:node="data">
<div>{{data.title}} - {{data.text}}</div>
</template>
</data-tree>
那么如何在組件內(nèi)傳遞Slot就是一個問題。
嵌套傳遞
通過固定級別的組件結(jié)構(gòu)里可以通過直接書寫<v-slot ...>來傳遞對應(yīng)的Slot元素,來實現(xiàn)一層一層的傳遞。
<data-tree>
<data-tree-item>
<template :node="data">
<slot :data="data"> xxx </slot>
</template>
</data-tree-item>
</data-tree>
通過在外層創(chuàng)建slot可以逐層將slot進(jìn)行傳遞,但是如果過多的嵌套層次,這樣就顯得很麻煩。
Render
還有一種方案是通過Render函數(shù)來進(jìn)行顯示,可以通過$slots來訪問當(dāng)前組件的slot元素,然后通過Render函數(shù)創(chuàng)建新組件時,將slot傳遞給下一層。
h('data-tree-item',{
scopedSlots: {
node: props => this.$slots.node(props)
},
})
這樣通過Render子元素就可以接受到對應(yīng)的Slot,也實現(xiàn)了傳遞。
動態(tài)組件
還有一種方式是通過動態(tài)組件,也是認(rèn)為比較推薦的實現(xiàn)方式,不是通過傳遞Slot,而是通過子節(jié)點主動去獲取根節(jié)點的Slot對象,然后直接在UI中渲染出來。
為此我們需要創(chuàng)建一個組件來渲染對應(yīng)的Slot對象。
首先需要獲取根節(jié)點:
const rootComponentName = 'data-tree'
/**
* 獲取父組件
*/
const getRootComponent = (
component: ComponentInternalInstance | null
): ComponentInternalInstance | undefined => {
if (component && component.type.name === rootComponentName) {
return component
}
if (component && component.parent) {
const parent = component.parent
return getRootComponent(parent)
}
}
通過遞歸我們可以獲取到對應(yīng)的父節(jié)點,這樣我們就可以把Slot作為Data暴露出來
setup(props) {
// 獲取根節(jié)點
const dataTree = getRootComponent(getCurrentInstance())
const parentSlots = dataTree?.slots
const nodeTemplate = parentSlots?.node as any
return {
nodeTemplate
}
}
這時候我們需要一個組件來渲染暴露出來的Slot:
components: {
TemplateContainer: {
functional: true,
props: {
template: {
type: Function
},
data: {
type: Object
}
},
render: (props, ctx) => h('div', [props.template(props.data)])
}
}
好了現(xiàn)在該準(zhǔn)備的都準(zhǔn)備好了,可以去實現(xiàn)UI的顯示了:
<template-container
v-if="nodeTemplate"
:template="nodeTemplate"
:data="node">
</template-container>
<template v-else>
{{ node.label }}
</template>
這樣我們就實現(xiàn)了類似下面定義Slot的傳遞,也解決了我們跨組件傳遞Slot的問題。
<slot :data="node" name="node">
{{ node.label }}
</slot>
本文使用的是Vue 3的事例,Vue 2也是相同的概念,在Vue 3中除了使用getRootComponent來查詢跟節(jié)點,也可以使用Provide/Inject來將Slot主動傳遞給子節(jié)點。
到此這篇關(guān)于Vue如何跨組件傳遞Slot的實現(xiàn)的文章就介紹到這了,更多相關(guān)Vue 跨組件傳遞Slot內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue使用動態(tài)組件手寫Router View實現(xiàn)示例
這篇文章主要為大家介紹了vue使用動態(tài)組件手寫RouterView實現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06
詳解如何使用 vue-cli 開發(fā)多頁應(yīng)用
本篇文章主要介紹了詳解如何使用 vue-cli 開發(fā)多頁應(yīng)用,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-12-12
vue3中使用vuex和vue-router的詳細(xì)步驟
這篇文章主要介紹了vue3中使用vuex和vue-router的步驟,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-12-12
一步步從Vue3.x源碼上理解ref和reactive的區(qū)別
vue3的數(shù)據(jù)雙向綁定,大家都明白是proxy數(shù)據(jù)代理,但是在定義響應(yīng)式數(shù)據(jù)的時候,有ref和reactive兩種方式,如果判斷該使用什么方式,是大家一直不很清楚地問題,下面這篇文章主要給大家介紹了關(guān)于從Vue3.x源碼上理解ref和reactive的區(qū)別的相關(guān)資料,需要的朋友可以參考下2023-02-02

