vuex的mutations如何内部调用getter?

export default new Vuex.Store({
  state: {
    cart_list:[1,2,3],
  },
  getters: {
    total: state => {
      var total = state.cart_list[0]+state.cart_list[1]+state.cart_list[2];
      return total;
    }
  },
  mutations: {
    get_total (state) { 
      console.log(">>>>>我的问题是:这里如何调用到 getters>total ?");
    }
  }
})

我现在的实现方法是,在调用的组件中用了一个:

this.$store.commit('get_total',this.$store.getters.total);

把total传入,这样做是对的么?

阅读 20.6k
3 个回答

getter 相当于 state 的计算属性,mutation 是用来修改 state 的。
mutation 直接修改 getter 不是正确操作啊。

新手上路,请多包涵

this.getters.total(state)
这就是一个对象内的内部调用

新手上路,请多包涵

可以通过action在提交commit的时候,把你要用的getter通过action的context.getters('getterName',obj),放在obj里面当参数传入,应该就可以了

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进