vuex-class-modules
vuex-class-modules copied to clipboard
how to reset the state of all modules?
for example, i want to clear the state on logout. how can i do this for all modules within the store?
Afaik there is no build-in way to reset a typescript object to its initial state.
I usually define a reset mutation and clear the state there manually.
In functions that would be smth like this
export default {
resetWholeState({commit}): void {
commit(RESET_WHOLE_STATE);
},
} as ActionTree<any, any>;
export default {
[RESET_WHOLE_STATE](state: any): void {
if (state.hasOwnProperty('_initialState')) {
Object.assign(state, merge({}, state._initialState, {_initialState: state._initialState}));
}
},
} as MutationTree<any>;
export const defaultState = (): IStrAny => merge({}, common(), local) as IStrAny;
export default () => merge({}, defaultState(), {_initialState: defaultState()}) as IStrAny;
should give an idea how to make it with classes, smth like
abstract class ResetModule {
public _initialState: object = {};
@Action
public resetWholeState() : void {
this.RESET_WHOLE_STATE();
}
@Mutation
protected RESET_WHOLE_STATE (){
this = this._initialState;
}
}