个人的一些粗浅总结,不完全正确,只是为了方便理解:
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); // 奇数