为了账号安全,请及时绑定邮箱和手机立即绑定

vuex 中的getter action mutations commit 对应什么功能作用,

vuex 中的getter action mutations commit 对应什么功能作用,

汪汪一只猫 2018-08-30 17:37:35
如题vuex 中的getter action mutations commit 对应什么功能作用,
查看完整描述

2 回答

?
Alice_hhu

TA贡献3条经验 获得超1个赞

个人的一些粗浅总结,不完全正确,只是为了方便理解:

getter (相当于 store 的计算属性,类似 vue 中的 computed,一般是对 state 中的属性处理过后的属性)

mutations (变化、方法、事件,类似 vue 中的 methods,可以对 state 中的属性做一些处理等,不能直接调用,需要 commit 触发调用)

action (用于 触发 mutation,即进行 commit 动作,而 action 是通过 store.dispatch 方法来触发)

例如:

const store = new Vuex.Store({
    state (){
      return {
        data1: 0
    };
  },
  getters: {
    getter1: state => {
      return state.data1 % 2 == 0 ? '偶数' : '奇数';
    }
  },
  mutations: { 
    fun1 (state){
      state.data1 ++;
    }
  },
  actions: {
      action1 (context){
          context.commit('fun1');
      }
  }
});
// 访问 getters
console.log(store.getters.getter1); // 偶数
// 分发 action
store.dispatch('action1');
console.log(store.getters.getter1); // 奇数


查看完整回答
反对 回复 2018-10-09
  • 2 回答
  • 0 关注
  • 1949 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信