Nx: Provide DataPersistence service in app.module.ts

Created on 9 Mar 2018  路  4Comments  路  Source: nrwl/nx

Problem:
After initializing the ngrx setup the DataPersistence service is not provided by the providers in app.module.ts but the AppEffects service is.

If I run my project I get an error because DataPersistence is used but nit provided.

zone.js:672 Unhandled Promise rejection: StaticInjectorError[DataPersistence]: 
  StaticInjectorError[DataPersistence]: 
    NullInjectorError: No provider for DataPersistence! ;

Solution:
After initializing the ngrx setup also add DataPersistence service to providers

bug

Most helpful comment

Before:

@NgModule({
  imports: [
    BrowserModule,
    RouterModule.forRoot(),
    StoreModule.forRoot(
      { app: appReducer },
      {
        initialState: { app: appInitialState },
        metaReducers: !environment.production ? [storeFreeze] : []
      }
    ),
    EffectsModule.forRoot([AppEffects]),
    !environment.production ? StoreDevtoolsModule.instrument() : [],
    StoreRouterConnectingModule
  ],
  declarations: [
    AppComponent,
  ],
  providers: [AppEffects],
  bootstrap: [AppComponent]
})

After:

@NgModule({
  imports: [
    BrowserModule,
    RouterModule.forRoot(),
    StoreModule.forRoot(
      { app: appReducer },
      {
        initialState: { app: appInitialState },
        metaReducers: !environment.production ? [storeFreeze] : []
      }
    ),
    EffectsModule.forRoot([AppEffects]),
    !environment.production ? StoreDevtoolsModule.instrument() : [],
    StoreRouterConnectingModule
  ],
  declarations: [
    AppComponent,
  ],
  // I provided DataPersistence here
  providers: [AppEffects, DataPersistence],
  bootstrap: [AppComponent]
})

All 4 comments

@BioPhoton - plz show the code samples before and after.

Before:

@NgModule({
  imports: [
    BrowserModule,
    RouterModule.forRoot(),
    StoreModule.forRoot(
      { app: appReducer },
      {
        initialState: { app: appInitialState },
        metaReducers: !environment.production ? [storeFreeze] : []
      }
    ),
    EffectsModule.forRoot([AppEffects]),
    !environment.production ? StoreDevtoolsModule.instrument() : [],
    StoreRouterConnectingModule
  ],
  declarations: [
    AppComponent,
  ],
  providers: [AppEffects],
  bootstrap: [AppComponent]
})

After:

@NgModule({
  imports: [
    BrowserModule,
    RouterModule.forRoot(),
    StoreModule.forRoot(
      { app: appReducer },
      {
        initialState: { app: appInitialState },
        metaReducers: !environment.production ? [storeFreeze] : []
      }
    ),
    EffectsModule.forRoot([AppEffects]),
    !environment.production ? StoreDevtoolsModule.instrument() : [],
    StoreRouterConnectingModule
  ],
  declarations: [
    AppComponent,
  ],
  // I provided DataPersistence here
  providers: [AppEffects, DataPersistence],
  bootstrap: [AppComponent]
})

@BioPhoton
Just looking for some more information here. Was this workspace created with create-nx-workspace or converted from a ng new workspace? This is probably a bug with the latter method.

Thank you

Can reproduce this with npm init nx-workspace name and adding ngrx via @nrwl/schematics in angular-console. At first I created an empty root store, afterwards I added a non-empty feature store. @BioPhoton's solution working.

  • @nrwl/nx: 7.8.0
  • @nrwl/builders: 7.8.0
  • @nrwl/schematics: 7.8.0
Was this page helpful?
0 / 5 - 0 ratings

Related issues

olakara picture olakara  路  3Comments

jon301 picture jon301  路  3Comments

MichaelWarneke picture MichaelWarneke  路  3Comments

zpydee picture zpydee  路  3Comments

vimalraj-a picture vimalraj-a  路  3Comments