Search code examples
angulartypescriptangular-moduleangular-library

How to use service inside import decorator for angular module config?


I want to method in service for module config, but module is loaded from decorator, and service is not DI yet. How to use service inside load module?

I use this answer for example

For example method in service is a Observeable, here is the config

export interface TranslateConfig {
  someProp?: string;
  anotherProp?: string;
  observeableProp?: Observeable<string>
}

And then to load module in app.module.ts

@NgModule({
  imports: [
    TranslateModule.forRoot({
      someProp: someValue,
      anotherProp: anotherValue,
      observeableProp: (method from service)
    })
  ]
})
export class AppModule{
  ...
}

I want to use service inside decorator


Solution

  • Already solved. There is no way to use service inside decorator.

    To solve this issue, create new service inside ConfigurableModule.

    @Injectable({
      providedIn: 'root'
    })
    export class AuthSerivce{
    
      constructor() { }
    
      private authFunction: Observable<string>;
    }
    

    In your app.module.ts

    @NgModule({
      ...
      imports: [
        ConfigurableModule.forRoot({
          YOUR CONFIG HERE
        }),
      ],
    ...
    })
    export class AppModule {
      constructor(
        private configurableModuleAuthService: ConfigurableModuleAuthService
      ) {
        configurableModuleAuthService.authFunction = of("YOUR AUTHENTICATION OBSERVABLE METHOD HERE");
      }
    }
    

    To use this service inside ConfigurableModule, it is the same with normal DI service