简体   繁体   中英

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)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM