Vuex getter 未更新 [英] Vuex getter not updating
问题描述
我有以下吸气剂:
withEarmarks: state => {
var count = 0;
for (let l of state.laptops) {
if (l.earmarks.length > 0) {
count++;
}
}
return count;
}
在一个组件中,这个计算属性派生自那个 getter:
And in a component, this computed property derived from that getter:
withEarmarks() { return this.$store.getters.withEarmarks; },
返回的值是正确的,直到我更改了 notebooks 数组中的一个元素,然后 getter 不会更新.
The value returned is correct, until I change an element within the laptops array, and then the getter doesn't update.
推荐答案
在你的情况下 state.laptops.earmarks
是一个数组,你正在通过它的数组索引 state 来操作它.笔记本电脑[索引]
.Vue 无法对状态数组的变化做出反应(通过索引).该文档为此提供了 2 种解决方法:
In your case state.laptops.earmarks
is an array, and you are manipulating it by its array index state.laptops[index]
. Vue is unable to react to mutations on state arrays (by index). The documentation provides 2 workarounds for this:
// 1. use purpose built vue method:
Vue.set(state.laptops, index, laptop)
// 2. splice the value in at an index:
state.laptops.splice(index, 1, laptop)
虽然它被记录在案,但我认为该页面上的一个巨大的霓虹灯发光标志说如果你不知道这一点,你将浪费数小时的生产力"将是一个很好的补充.
您可以在此处阅读有关此警告"的更多信息:https://vuejs.org/v2/guide/list.html#Caveats
You can read more about this "caveat" here: https://vuejs.org/v2/guide/list.html#Caveats
这篇关于Vuex getter 未更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!