英文:
vuejs - good practices when using getters and/or computed properties
问题
I love vue.js and I definitely love computed properties and VueX
getters. But I have come to a point where I am not sure if the way I am using them might have some drawbacks in terms of performance.
This is a common pattern throughout my code (same applies to local components data and computed properties):
From this simple state (source of truth):
export default () => ({
bonds: {
eth: [],
celo: []
}
})
I will usually go as follows within my getters:
export default {
getBondsEth(state) {
return state.bonds.eth
},
getBondsCelo(state) {
return state.bonds.celo
},
getTotalBondsEth(_, getters) {
return getters.getBondsEth.length
},
getTotalBondsCelo(_, getters) {
return getters.getBondsCelo.length
},
getTotalBonds(_, getters) {
return getters.getTotalBondsEth + getters.getTotalBondsCelo
},
getWhateverComputationEth(_, getters) {
return getters.getBondsEth.reduce... or map or filter or find etc....
}
...
}
So, my questions will be
-
这是否被认为是一种不良实践,因为我使用了依赖于其他getter的getter?这些是否被视为循环依赖?
-
我是否应该总是直接从源数据中派生我的getter?例如,将上述更改为...
getTotalBonds(state) {
return state.bonds.eth.length + state.bonds.celo.length
}
- 是否有一种方法可以使用浏览器控制台来调试我的getter或计算属性,以确保没有性能问题或奇怪的迭代循环等?
And thank you very much if you take the time to answer!
英文:
I love vue.js and I definitely love computed properties and VueX
getters. But I have come to a point where I am not sure if the way I am using them might have some drawbacks in terms of performance.
This is a common pattern throughout my code (same applies to local components data and computed properties):
From this simple state (source of truth):
export default () => ({
bonds: {
eth: [],
celo: []
}
})
I will usually go as follows within my getters:
export default {
getBondsEth(state) {
return state.bonds.eth
},
getBondsCelo(state) {
return state.bonds.celo
},
getTotalBondsEth(_, getters) {
return getters.getBondsEth.length
},
getTotalBondsCelo(_, getters) {
return getters.getBondsCelo.length
},
getTotalBonds(_, getters) {
return getters.getTotalBondsEth + getters.getTotalBondsCelo
},
getWhateverComputationEth(_, getters) {
return getters.getBondsEth.reduce... or map or filter or find etc....
}
...
}
So, my questions will be
-
Will this be considered a bad practice since I am using getters that depends on others? Are these considered circular dependencies?
-
Should I always derived my getters directly from the source of truth? For example, change the above to...
getTotalBonds(state) {
return state.bonds.eth.length + state.bonds.celo.length
}
- Is there a way to debug my getters or computed properties using the browser console so I can make sure I do not have performance issues or weird iteration loops, etc?
And thank you very much if you take the time to answer!
答案1
得分: 1
这不会造成循环依赖。只有在获取器 A
依赖于获取器 B
,而获取器 B
又依赖于获取器 A
时,才会出现循环依赖,这将导致无限递归。
获取器本身没有问题,但据我所知,Vue 在每个时刻都会调用它们(关于 时刻 的更多信息,请查看这里),这在大多数情况下都会浪费资源。因此,对于很少更改的值,建议使用 computed
,因为 computed
只会被调用一次,而Vue会缓存执行结果。
英文:
This wouldn't be a circular dependency. It would be circular dependency in case when getter A
would depend on getter B
and getter B
would depend on getter A
, which would create an infinite recursion.
Getters are fine, but AFAIK they get called by Vue on every tick (more on what the tick is here), which would be wasteful most of the time. So for rarely changing values the use computed
is encouraged, since computed
gets called only once and Vue caches the result of execution.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论