Store: Store has already been added app

Created on 14 Mar 2018  路  10Comments  路  Source: ngxs/store

Currently I have 2 stores, AppStore and AuthStore. AppStore is controlled by the AppModule for general all-purpose states for the app. AuthStore is managed by the AuthModule.

In AppModule:

  imports: [
    //...
    NgxsModule.forRoot([AppStore]),
    AuthModule,
    // ...
  ]

In AuthModule:

  imports: [
    //...
    NgxsModule.forFeature([AuthStore]),
    AuthModule,
    // ...
  ]

If I do this in AuthModule, I get an error:

Error: Store has already been added app
at _loop_1 (ngxs-api.js:77)
at StoreFactory.add (ngxs-api.js:98)
at StoreFactory.addAndReturnDefaults (ngxs-api.js:114)
at NgxsFeatureModule.initStores (ngxs-api.js:455)
at new NgxsFeatureModule (ngxs-api.js:451)
at _createClass (core.js:10895)
at _createProviderInstance$1 (core.js:10865)
at initNgModule (core.js:10818)
at new NgModuleRef_ (core.js:12073)
at createNgModuleRef (core.js:12062)

AuthModule isn't LazyLoaded. If I removed the import from AuthModule and add the AuthStore to the import in AppModule it works. I'd rather not add the AuthStore into the AppModule if I can help it.

Am I not doing this correctly? Does forFeature() only work if the module is lazy loaded?

core

Most helpful comment

@un33k - I'm down for trying that. @deebloo - this sounds like up your alley.

All 10 comments

forFeature is only for lazy loaded routes. You should import it in the forRoot in this case.

@amcdnl I am not sure about this. Ngrx had the same problem, lots of people nagged, they finally allowed empty forRoot([]) when they moved to ngrx/platform.

Not all features are meant to be lazyLoaded. A self-contained module should be able to take care of its own store and everything that goes with it.

For example, think of an Auth module. Just because it is put it its own module in a libs/ directory, doesn't mean that it has to be lazyLoaded. In this case, app.module.ts can import authModule without worrying about anything else.

I wonder if there are any restrictions that prevent forRoot([]) implementation?

@un33k - I'm down for trying that. @deebloo - this sounds like up your alley.

@yarrgh I think this may have been fixed by @leon work with the feature modules. can you check again with the latest rc?

With 2.0.0-rc.18 I get a different error.

'NgxsModule.forRoot()' was not called at the root module

I do have NgxsModule.forRoot([]), in my app.module.ts and immediately after I'm importing my AuthModule that imports NgxsModule.forFeature([AuthState]). If I remove the forFeature() import form my AuthModule it works (except for the AuthState not being set up, obviously).

Here's a StackBlitz demonstrating the error:

https://stackblitz.com/edit/ngxs-simple-amvqoh

@yarrgh the problen is with the feature. if you remove AuthModule from :

@NgModule({
  imports:      [ 
    BrowserModule, 
    NgxsModule.forRoot([CountState]),
    AuthModule ],
  declarations: [ AppComponent ],
  bootstrap:    [ AppComponent ]
})

works correctly.

The problem is:

@NgModule({
  imports: [
    CommonModule,
    NgxsModule.forFeature([AuthState]),
  ],
  declarations: [],
  exports: [],
  providers: []
})

You use AuthState with forFeature function, but AuthModule I not being loaded lazy loading

From @un33k's comment:

Not all features are meant to be lazyLoaded. A self-contained module should be able to take care of its own store and everything that goes with it.

I think forFeature() should work for non lazy loaded modules as well as lazy loaded modules. Having AuthModule be lazy loaded doesn't make much sense since it is always needed in every aspect of my app.

Maybe, if it is easier, have a forFeature() for non lazy loaded modules and a forLazyFeature() for lazy loaded modules?

@yarrgh I agree with you! forFeature() should work for non lazy loaded modules as well as lazy loaded modules.
it seems that's the bug

Closing this issue now that the fix has been released

Was this page helpful?
0 / 5 - 0 ratings