Reputation: 3251
I want to dispatch an action when ngrx store inits (@ngrx/store/init
). I created an Effect for that:
@Effect()
reloadConext$ = this.actions$.pipe(
ofType(INIT),
switchMap(() => {
console.log('INIT ACTION');
//dispatch my action here
return of([]);
}
The effect is not being fired when store init action is dispatched. I have effects modules for root registered in app.module :
EffectsModule.forRoot([AppEffects]),
If I remove the ofType
action filter the event is fired. Does anyone know what the filter for init action is not working?
Thanks in advance.
Upvotes: 10
Views: 10926
Reputation:
I think you are looking for the ROOT_EFFECTS_INIT or the onInitEffects lifecycle method for non root effects.
ROOT_EFFECTS_INIT from the docs:
@Effect()
init$ = this.actions$.pipe(
ofType(ROOT_EFFECTS_INIT),
map(action => ...)
);
onInitEffects from the docs:
class UserEffects implements OnInitEffects {
ngrxOnInitEffects(): Action {
return { type: '[UserEffects]: Init' };
}
}
Upvotes: 17