0
votes

I'm working with ngrx and I write an Effect. The problem is that I want to use the effects with EffectModule.forFeature, but it only works for me when I register it forRoot. I tried to register both, but only when I register forRoot in app.module, the effect work. Do I need to register it both in the app.module and in my component module?

imports: [
  CommonModule,
  RouterModule,
  DataTableModule,
  ModalModule,
  SelectListModule,
  StoreModule.forFeature('estateOwners', EstateOwnerReducer),
  EffectsModule.forFeature([EstateOwnersEffects]),
]
1

1 Answers

2
votes

You will have to add the EffectsModule to app module also. According to the docs at https://ngrx.io/guide/effects in the Step 2 it is mentioned that:

Register the EffectsModule in your application root imports. This EffectsModule must be added to your root NgModule for the effects providers to be registered and start when your application is loaded.

If you only want to have EffectsModule for features and not available for root, then you can do:

app.module.ts

import { EffectsModule } from '@ngrx/effects';

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

and in your module you can have the same code:

imports: [
  CommonModule,
  RouterModule,
  DataTableModule,
  ModalModule,
  SelectListModule,
  StoreModule.forFeature('estateOwners', EstateOwnerReducer),
  EffectsModule.forFeature([EstateOwnersEffects]),
]

Also there is a Note in the docs as:

Note: Running an effects class multiple times, either by forRoot() or forFeature(), (for example via different lazy loaded modules) will not cause Effects to run multiple times. There is no functional difference between effects loaded by forRoot() and forFeature(); the important difference between the functions is that forRoot() sets up the providers required for effects.