状态突变的Vuex方法如下:
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 };
}
答案 0 :(得分:3)
那么,你在找这样的东西吗?
<div id="app">
{{ $store.state.id }}
<br>
{{ $store.state.name }}
<br>
{{ $store.state.last }}
</div>
<script src="https://unpkg.com/vue"></script>
<script src="https://unpkg.com/vuex"></script>
&#13;
a-sky
&#13;