Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between StoreModule.forRoot() and StoreModule.forFeature()

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?

like image 814
Shanmugam M Avatar asked Sep 21 '17 15:09

Shanmugam M


People also ask

What is Storemodule forRoot?

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.


2 Answers

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.

like image 83
dee zg Avatar answered Sep 17 '22 21:09

dee zg


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.

like image 25
Shanmugam M Avatar answered Sep 18 '22 21:09

Shanmugam M