How to reset all states of ngrx/store?

后端 未结 4 737
执笔经年
执笔经年 2020-12-15 17:11

I am using Angular 2 with ngrx/store. I want to reset the whole store states when user dispatch USER_LOGOUT.

I read the Dan Abramov\'s answer of How to

4条回答
  •  既然无缘
    2020-12-15 18:00

    With @ngrx/store": "^4.0.3" this is slightly different because there are small changes, so my 'clear state' looks like this

    import { ActionReducerMap } from '@ngrx/store';
    import { ActionReducer, MetaReducer } from '@ngrx/store';
    
    export const rootReducer: ActionReducerMap = {
      points: pointsReducer,
      ...
    };
    
    export function clearState(reducer: ActionReducer): ActionReducer {
      return function(state: StoreStates, action: Action): StoreStates {
        if (action.type === 'CLEAR_STATE') {
          state = undefined;
        }
        return reducer(state, action);
      };
    }
    export const metaReducers: MetaReducer[] = [clearState];
    

    and

    import { StoreModule } from '@ngrx/store';
    import { metaReducers, rootReducer } from '../root.reducer';
    
    export const imports: any = [
       StoreModule.forRoot(rootReducer, { metaReducers }),
       ...
    ]
    

提交回复
热议问题