我已经看到了同样的奇怪的here,但是由于我使用了ngrx9,我认为应该有一些不同的解决方案。我在应用模块中的代码和还原器如下所示
.....
StoreModule.forRoot(
reducers,
{
metaReducers,
runtimeChecks: {
strictStateImmutability: true,
strictActionImmutability: true
}
}),
...我进口的减速器是
export const reducers: ActionReducerMap<AppState> = {
[friendsReducer.friendsFeatureKey]: friendsReducer.reducer,
[authReducer.authFeatureKey]: authReducer.reducer,
...
};
export const metaReducers: MetaReducer<AppState>[] = !environment.production ? [] : [];我的LOGOUT操作存储在auth.actions中。那么,我应该如何包括根还原器来重置所有状态呢?
发布于 2020-07-27 11:22:20
只需添加metareducer以清除状态即可。
export function clearOnLogoutMetaReducer(reducer) {
return function(state, action) {
if(action.type === logout.type) {
return reducer(undefined, action);
}
return reducer(state, action);
}
} 并将其添加到元还原器数组中。
发布于 2020-09-29 23:33:58
公认的答案是我经常在StackOverflow上看到的建议,不幸的是,这是你永远不应该做的事情。
问题
如果将状态设置为潜在的未定义状态,将遇到以下几个问题:
您连接到存储区的任何
如果
解决方案
要清除存储,您需要分派一个操作,该操作将简化为一个新的初始状态对象。ngrx/redux最重要的一点是,动作是沿着单个流分派的。这意味着一个特性存储中的减缩器(在您的例子中是“朋友”)可以在另一个存储中侦听操作(在您的例子中是'auth')。因此,在您的friends.reducer.ts中执行如下操作:
import * as AuthActions from 'wherever/your/auth/actions/are';
// Replace this with actual interface
export interface FriendsState {
friends: string[];
}
// Replace this with actual object
export const initialFriendsState:FriendsState = {
friends: [],
}
export const reducer = createReducer(
initialFriendsState,
on(AuthActions.LOGOUT, (state: FriendsState) => ({...initialFriendsState})),
on(FriendsActions.GET_FRIENDS (state: FriendsState, {friends}) => ({...state, friends})
//...whatever other reducers you have in your friends store
);https://stackoverflow.com/questions/63114137
复制相似问题