Recently ngrx store changed the way to register the store variables in an angular application.
What is the difference between StoreModule.forRoot() and StoreModule.forFeature()
Do we need to register both to make the application work?
The forRoot method is invoked in the AppModule and, generally, once in the application to initialize the Store and provide the initial reducers/actions/state configuration.
Its used with lazy loaded reducers. When you have (lazy loaded) feature modules and you want to register reducers within that module, then you use forFeature
. Otherwise, in your AppModule
you use forRoot
.
Always Import forRoot() calls I think this is probably pretty obvious, but just for the record, you’ll need to make sure you’ve already imported the root store and effects module in your main application.
imports: [ StoreModule.forRoot({}), EffectsModule.forRoot([]), ...
If you have reducers or effects that apply at this level, you should add them in here, but even if you don’t have any reducers or effects at the root level of your application, you need to make these two calls. Otherwise, the forFeature() calls won’t be able to access the root store or effect location to add in the reducers and effects for the feature.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With