4

当我向我的状态添加一些新数据时,新数据不会出现在我的日志或 DevTool Ui 中。

是否有一种机制来重置状态,以便数据出现。似乎仍在显示某些缓存中的旧数据。

谢谢

4

4 回答 4

2

在 v3.1 中reset,商店中有一个功能可以让您重置状态,请参阅此处的文档

于 2018-07-06T07:47:48.713 回答
2

我更喜欢使用这个重置插件;https://github.com/ng-turkey/ngxs-reset-plugin

它是一个 ngxs 插件,易于实现。

于 2019-02-19T07:13:38.137 回答
1

您可以简单地向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';
}

现在您可以在您的serviceor中分​​派操作以component轻松重置NGXS状态,如下所示:

constructor(private store: Store) {}

yourMethod(): void {
    this.store.dispatch(new ResetAppState());
}
于 2021-08-18T09:35:28.597 回答
0

对于状态重置,我尝试通过插件(https://ngxs.gitbook.io/ngxs/advanced/meta-reducers)使用“meta-reducer”概念,但我无法将每个切片的状态重新初始化为默认值.

于 2018-06-26T05:37:34.860 回答