redux ROOT_EFFECTS_INIT在初始化存储时不被触发

jyztefdp  于 12个月前  发布在  其他
关注(0)|答案(2)|浏览(76)

我有一个初始化效果的效果=>

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

我在文件的底部添加了这个,我还试图包括一个defer,但当我的商店初始化时,这个效果从未被触发。
我检查了ngrx的repo,但是我应用的解决方案没有触发我的函数。
我做错什么了吗

nbysray5

nbysray51#

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

yourAction.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上看到了这个

amrnrhlw

amrnrhlw2#

为了使事件派遣你必须登记你的影响:

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

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

相关问题