首页 > 解决方案 > ngrx:存储初始化操作的效果

问题描述

我想在 ngrx 存储初始化(@ngrx/store/init)时调度一个动作。我为此创建了一个效果:

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

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

EffectsModule.forRoot([AppEffects]),

如果我删除ofType动作过滤器,事件就会被触发。有谁知道初始化操作的过滤器不起作用?

提前致谢。

标签: angularngrxngrx-storengrx-effects

解决方案


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

文档中的 ROOT_EFFECTS_INIT :

@Effect()
init$ = this.actions$.pipe(
  ofType(ROOT_EFFECTS_INIT),
  map(action => ...)
);

onInitEffects 来自文档:

class UserEffects implements OnInitEffects {
  ngrxOnInitEffects(): Action {
    return { type: '[UserEffects]: Init' };
  }
}

推荐阅读