ngrx:存储 INIT 操作的效果

Luc*_*tos 5 ngrx ngrx-effects angular ngrx-store

我想在 ngrx store inits ( @ngrx/store/init)时分派一个动作。我为此创建了一个效果:

    @Effect()
  reloadConext$ = this.actions$.pipe(
    ofType(INIT),
    switchMap(() => {
      console.log('INIT ACTION');
        //dispatch my action here
        return of([]);
    }
Run Code Online (Sandbox Code Playgroud)

调度 store init 操作时不会触发效果。我在 app.module 中注册了 root 的效果模块:

EffectsModule.forRoot([AppEffects]),
Run Code Online (Sandbox Code Playgroud)

如果我删除ofType操作过滤器,则会触发事件。有谁知道 init 操作的过滤器不起作用?

提前致谢。

小智 12

我认为您正在寻找非根效果的ROOT_EFFECTS_INITonInitEffects生命周期方法。

ROOT_EFFECTS_INIT 来自文档:

@Effect()
init$ = this.actions$.pipe(
  ofType(ROOT_EFFECTS_INIT),
  map(action => ...)
);
Run Code Online (Sandbox Code Playgroud)

来自文档的 onInitEffects:

class UserEffects implements OnInitEffects {
  ngrxOnInitEffects(): Action {
    return { type: '[UserEffects]: Init' };
  }
}
Run Code Online (Sandbox Code Playgroud)