2016-11-29 85 views
9

我有下面的吸气剂:Vuex吸气剂不更新

withEarmarks: state => { 
    var count = 0; 
    for (let l of state.laptops) { 
     if (l.earmarks.length > 0) { 
      count++; 
     } 
    } 
    return count; 
} 

而在一个部件,该计算属性从吸气剂衍生自:

withEarmarks() { return this.$store.getters.withEarmarks; }, 

返回的值是正确的,直到我改变元素在笔记本电脑阵列中,然后getter不会更新。

+0

你是如何做的'laptops'数组中的变化,可以包含这些代码。 – Saurabh

+0

@saurabh我已经尝试了这两种方式:'state.laptops [index] = laptop;'和'state.laptops [index] = Object.assign({},laptop);' – daninthemix

回答

21

在你的情况下,state.laptops.earmarks是一个数组,你正在操纵它的数组索引state.laptops[index]。 Vue无法对状态数组上的突变做出反应(按索引)。该文档提供了2个解决方法是:

// 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