How to isolate @ngrx effects to specific route url

546 Views Asked by At

I want to isolate bunch of @ngrx effects in my lazy loaded module so, that they can only be triggered if module related url is active. So if I understand correctly, effects are applied globally, and they are not depend on url, even if my module is lazy loaded, when this url is activated. So in every effect I write something like this

  routeRelatedEffect$ = createEffect(() =>
    this.actions$.pipe(
      ofType(someAction),
      withLatestFrom(this.store.pipe(select(selectRouterState))),
      filter((rs) => rs.state.url.startsWith("/my-url"))
      // ...
    )
  );

Here I check url. I wonder if there is another approach to do this? Maybe some sort of global filter? How is the right way to do this?

1

There are 1 best solutions below

1
On

Effects can be added specifically to lazy loaded module.


//app.module.ts -- add to root component
EffectsModule.forRoot([NotifyEffects, ChatListEffects])
//lazy loaded module
EffectsModule.forFeature([LazyLoadedEffect])

If you want to have access to routes i suggest you to explore @ngrx/router-store https://ngrx.io/guide/router-store