vue3中computed的如下使用方式是否有不妥?

使用的 pinia
按官方的说法,getter相当于computed

那么我这样用:

const curr_data = computed(() => {

return store[`curr_${props.name}_data`].children

})

是不是嵌套computed?
我这样写唯一的目的就是简化 template 中的代码,这样做有什么缺点吗?

谢谢。


回答:

本质上来说这样用没什么问题,但更好的方式是直接在 pinia 中定义成一个 getter,在外部直接访问即可,也就不需要先定义在 render function 外部,又怕丢失响应性包一层 computed

// store

const store = defineStore('store', {

state: ...,

getter: {

currentData: (state) => state[`curr_${props.name}_data`].children

},

actions: ...

})


回答:

挺好的,主要自己看得懂,而且能给人讲明白为什么这么写就行。


回答:

这样做其实没有什么太大的意义,你这样写的目的是为了监听store的值,如果store的值发生变化则触发computed事件,其实getter中获取的值已经是一个响应式的了,你这里写的computed事件只是监听了一个store的数据变化并返回一个新的数据,内部没有做任何逻辑操作,那为何不直接使用getter呢

以上是 vue3中computed的如下使用方式是否有不妥? 的全部内容, 来源链接: utcz.com/p/935273.html

回到顶部