更新Vuex商店属性

时间:2018-12-17 20:54:45

标签: vue.js vuex

我有一个与此类似的模块:

export default {
  namespaced: true,
  state: {
    myObj: [
     {id:'1', value:'foo'}
     ...
    ]
  }
  mutations: {
    test(state, {id, newValue}){
      for (let x of state.myObj){
        if (x.id === id){
          x.value=newValue;
        }
      }  
    }
  }
};

如何触发Vuex以与添加或删除myObj行相同的方式自动更新此字段?我知道Vue.set方法,但是我不确定在这种情况下如何使用它。我还阅读了有关观察者的信息,但是我不确定它会去哪里以及如何连接。

这是正确的吗?

new Vue({
  watch: {
    "$store.someModule.state": {
       deep:true,
       handler() { console.log("Why do I need a console log here?");}
    }
  ...
})

0 个答案:

没有答案
相关问题