2

I need to be able to inject and use a angular 2 service (called SomeService below) from the imports section of NgModule:

...
imports: [
    BrowserModule,
    RouterModule.forRoot(AppRoutes, {useHash: true}),
    HttpModule,
    ReactiveFormsModule,
    NgbModule,
    StoreModule.provideStore({
        currentUserAccount: someFunction(currentUserAccountReducer, someService),
        authenticated: someFunction(authenticatedReducer, someService)
      },
      {
        authenticated: false
      })
  ],
  ...

I need that because I need to use a fully functional service (that depends on Http) from a plain function (named someFunction above) in order to rehydrate a ngrx store application.

Here someFunction is a meta reducer.

See concept of meta reducer in ngrx store application.

Can someone please help?

balteo
  • 23,602
  • 63
  • 219
  • 412

1 Answers1

1

Not completely sure I understand what you are trying to do (without seeing more code), but I imagine what you are trying to do can be done by using a factory for the provider configuration

providers: [
  {
    provide: WhateverService,
    useFactory: (things: Things, to: To, inject: Inject) => {
      // Not 100% sure, but I believe the return should be 
      // synchronous. If you have some asynchronous actions
      // to be resolved first, you may just want to pass the 
      // Promise or Observable to the constructor
      return new WhateverService(...);
    },
    deps: [Things, To, Inject]
  }
]

In your StoreModule, it could be something like

@NgModule({})
export class StoreModule {
  static provideStore(variable) {
    return {
      ngModule: StoreModule,
      providers: [
        {
          provide: WhateverService,
          useFactory: (things: Things, to: To, inject: Inject) => {
            // use variable here
            return new WhateverService(...);
          },
          deps: [Things, To, Inject]
        }
      ]
    }    
  }
}

Another option, if you are trying to resolve some remote data before bootstrap, is to do something like this. Other than that, I may be completely off, as I am not familiar with ngrx.

Community
  • 1
  • 1
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
  • I used the sample from the linked reply http://stackoverflow.com/a/39454713/536299. Thanks a lot. – balteo Sep 23 '16 at 12:37