如何查看嵌套的 Vuex 存储值
我有一个 Vue 项目,我需要在其中查看一些 Vuex 存储值。这是代码:
computed: {
serverCategories: {
get() {
return this.$store.state[this.storeName].serverFilters.categories[0].value.name;
},
set(value) {
return value;
},
}
},
watch: {
serverCategories: {
deep: true,
handler(newVal, OldVal) {
console.log(newVal);
}
}
},
但它不会对更改做出反应。尽管存储状态中的值已更改,但 serverCategories 仍包含相同的值。这段代码有什么问题?
I have a Vue project where I need to watch some Vuex store values. Here is the code:
computed: {
serverCategories: {
get() {
return this.$store.state[this.storeName].serverFilters.categories[0].value.name;
},
set(value) {
return value;
},
}
},
watch: {
serverCategories: {
deep: true,
handler(newVal, OldVal) {
console.log(newVal);
}
}
},
But it does not react on changes. serverCategories still contains the same value although the value in store state is changed. What is wrong with this code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用 vuex
mapGetters
,将其包装在计算属性中(请参阅 此处的文档)我强烈建议您每次需要访问商店时都使用 getter,而不是
$store.state.myStore.myValue
。您还可以使用 vuex 来改变数据(通过
mutations
和 vuex 函数mapMutations
或通过使用mapActions
的actions
)示例:
存储
Vue 组件
You can use the vuex
mapGetters
which you can wrap in your computed properties (see documentation here)I would highly recommand using getters each time you need to access the store instead of
$store.state.myStore.myValue
.You can also use vuex to mutate your data (through
mutations
and the vuex functionmapMutations
or thoughactions
usingmapActions
)Example :
store
Vue component