初始化商店时不会触发ROOT_EFFECTS_INIT

问题描述 投票:0回答:2

我有一个带有 Init Effect 的效果 =>

  @Effect()
  init$ = this.actions$.pipe(
    ofType(ROOT_EFFECTS_INIT),
    map(action =>
      new featureActions.GetAllMissionRequest({
        projectID: environment.projectId,
        projectContextID: environment.projectId
      })
    )
  );

我将其添加到文件底部,我还尝试包含延迟,但当我的商店初始化时,永远不会触发此效果。

我检查了 ngrx 的存储库,但我应用的解决方案没有触发我的功能。

我做错了什么吗?

redux ngrx
2个回答
3
投票

对于这种情况,您可以在效果上实现 OnInitEffects :

你的Action.ts

export const initEffect = createAction('[ModuleToto] init effect');

yourEffect.ts

@Injectable()
export class YourEffect implements OnInitEffects {
  ngrxOnInitEffects(): Action {
    return initEffect();
  }

  init$ = createEffect(
    () => this.actions$.pipe(
      ofType(initEffect),
      map(action =>
        new featureActions.GetAllMissionRequest({
          projectID: environment.projectId,
          projectContextID: environment.projectId
        })
      )
    )
  );

}

我在 https://ngrx.io/guide/effects/lifecycle#oniniteffects

上看到了这个

0
投票

为了调度事件,您必须注册您的效果:

@NgModule({
  imports: [
    EffectsModule.forRoot([MovieEffects])
  ],
})
export class AppModule {}

https://ngrx.io/guide/effects#registering-root-effects

© www.soinside.com 2019 - 2024. All rights reserved.