当我们使用不同的reducer时,重设整个状态的最佳方法是什么? 我在使用选择器时遇到了问题,因为在进行重置时它不会带给我初始状态
MetaReducer
import { ActionReducer } from '@ngrx/store';
import { UserActionTypes } from '../../actions/actionTypes';
export function clearStateReducer(reducer: ActionReducer<any>): ActionReducer<any> {
return (state, action) => {
if (action.type === UserActionTypes.Logout) {
state = undefined;
}
return reducer(state, action);
};
}
状态模块
import { NgModule } from '@angular/core';
import { StateService } from './state.service';
import { StoreDevtoolsModule } from '@ngrx/store-devtools';
import { StoreModule } from '@ngrx/store';
import { appReducer } from './reducers';
import { getInitialState } from './state';
import { metaReducers } from './reducers/metaReducers';
@NgModule({
imports: [
StoreModule.forRoot(appReducer, {
initialState: getInitialState(),
metaReducers,
runtimeChecks: {
strictStateImmutability: false,
strictActionImmutability: false,
strictStateSerializability: false,
strictActionSerializability: false
}
}),
StoreDevtoolsModule.instrument({
maxAge: 25
})
],
providers: [StateService]
})
export class StateServiceModule {}
答案 0 :(得分:0)
您可以通过将状态设置回初始状态来重置
export const postReducer = createReducer(
initialState,
on(PostActions.ResetState, (state) => {
return { state: initialState };
}),
);
答案 1 :(得分:0)
您可能没有分配状态,而是用undefined
调用了reducer:
export function clearStateReducer(reducer: ActionReducer<any>): ActionReducer<any> {
return (state, action) => {
if (action.type === UserActionTypes.Logout) {
return reducer(undefined, action);
}
return reducer(state, action);
};
}