vuex-class-modules icon indicating copy to clipboard operation
vuex-class-modules copied to clipboard

how to reset the state of all modules?

Open pancake-boy opened this issue 5 years ago • 2 comments

for example, i want to clear the state on logout. how can i do this for all modules within the store?

pancake-boy avatar Jan 29 '20 07:01 pancake-boy

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.

bodograumann avatar Feb 11 '20 14:02 bodograumann

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;
}
}

BonBonSlick avatar Dec 15 '21 01:12 BonBonSlick