vuex reset state

一般写法

1
2
3
4
5
6
const state = {
a: 1,
b: { c: 1}
};

export default { state };

如果有重置状态的需求

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
const defaultState = () => ({
a: 1,
b: { c: 1}
});

const state = defaultState();

const actions = {
reset({commit}) {
commit('reset');
}
};

const mutations = {
reset(state) {
const initState = defaultState();
Object.keys(state).forEach( x => {
state[x] = initState[x];
});
}
};

export default { state, actions, mutations };
#

Comments

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×