getter方法的作用 vuex_vuex 中的getter action mutations commit 对应什么功能作用,

15a8b657631e346db6691dc6f19c5d64.png

Alice_hhu

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值