如何合并两个组合函数的状态?



如何合并两个组合函数的状态? 我读过文档,没有找到任何示例?

function useFeature1() {
let state = reactive({
todo: '',
todolist: [],
})
return {
state
}
}
function useFeature2() {
let state = reactive({
items: []
})
return {
state
}
}

我刚刚明白合并状态是没有意义的。可能最好只是一致地命名状态。像mainStat,stateWithFeature等。

...
export default {
setup() {
const mergedState = { ...useFeature1(), ...useFeature2() }
// do stuff with merged     
}
}
function useFeature1() {
const state = reactive({
todo: '',
todolist: [],
})
return toRefs(state)
}
function useFeature2() {
const items = ref([])
return { items }
}

但是为什么?要访问模板中的反应式数据,您必须在设置中返回一个对象 - 无需合并。

最新更新