当我向我的状态添加一些新数据时,新数据不会出现在我的日志或 DevTool Ui 中。
是否有一种机制来重置状态,以便数据出现。似乎仍在显示某些缓存中的旧数据。
谢谢
在 v3.1 中reset
,商店中有一个功能可以让您重置状态,请参阅此处的文档
我更喜欢使用这个重置插件;https://github.com/ng-turkey/ngxs-reset-plugin
它是一个 ngxs 插件,易于实现。
您可以简单地向app.state.ts
文件中添加一个操作,如下所示
重置状态的动作
@Action(ResetAppState)
resetState(ctx: StateContext<AppStateModel>): Observable<AppStateModel> {
return of(ctx.getState()).pipe(
map(currentState => {
ctx.patchState({/* enter initial/reset values here */});
return currentState;
})
);
}
您还必须将操作添加到app.action.ts
文件中
export class ResetAppState {
static readonly type = '[AppState] Reset App State';
}
现在您可以在您的service
or中分派操作以component
轻松重置NGXS
状态,如下所示:
constructor(private store: Store) {}
yourMethod(): void {
this.store.dispatch(new ResetAppState());
}
对于状态重置,我尝试通过插件(https://ngxs.gitbook.io/ngxs/advanced/meta-reducers)使用“meta-reducer”概念,但我无法将每个切片的状态重新初始化为默认值.