1
的狀態突變Vuex方法是如下:vuex如何改變store中的多個屬性,如在redux中?
const store = new Vuex.Store({
state: {
fetching: false,
board: null
},
mutations: {
setFething (state) {
state.fetching = true
},
setCurrentBoard (state, board) {
state.board = board
state.fetching = false
}
}
})
但我擔心它會引發兩個轉變爲board
和fetching
每個屬性獨立而不是一個,我的觀點將被更新雙重倍。這只是一個簡單的例子,我有更復雜的屬性突變,會更好地突變一個突變。 vuex有可能嗎?
我喜歡Redux的方法返回突變只有一次狀態對象:
initialState = { board: null, fetching: false };
export default function reducer(state = initialState, action = {}) {
switch (action.type) {
case Constants.SET_FETCHING:
return { ...state, fetching: true };
case Constants.SET_CURRENT_BOARD:
return { ...state, ...action.board, fetching: false };
}
使用Vuex acrions。創建您同時執行多項更改的操作。 – WaldemarIce
這種情況是我不能一次返回新的不可變狀態對象,但我必須分別改變每個屬性。 – luzny