Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple ngrx store in one application

We are developers of a module of a big Angular application. Modules are independent from each other, they are developed by separate teams. We would like to use an ngrx store in our module.

An other module already has an ngrx store. If I try to add a store to our module.ts in the usual way:

@NgModule({
  imports: [
    ...
    StoreModule.provideStore( ... )
  ],
  ...

it breaks the whole app. Is there any way to provide a separate store for our module?

(app uses ngrx2)

like image 990
elcsiga Avatar asked Nov 07 '22 15:11

elcsiga


1 Answers

You can divide the store up, for one you can have a core store to load with app module at start up using forRoot, also adding metaReducers and initialState to it

StoreModule.forRoot(reducers, {metaReducers: metaReducers, initialState: getInitialState})

and for lazyloaded modules you can add them later to the store using

StoreModule.forFeature('gallery', reducers, {initialState: getGalleryInitialState})

lazy-loading is a new feature for ver-4, I don't think ver-2 supports dividing the store states, I upgrade just for the Selectors they help reduce code and improve performance read more about them here

like image 115
ramon22 Avatar answered Nov 14 '22 20:11

ramon22