如題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);?//?奇數添加回答
舉報
0/150
提交
取消
