Search code examples
angularconfigurationbootstrappingangular-libraryangular2-bootstrapping

How to pass configuration parameters to angular libraries?


I am building a wrapper library 'auth-lib' for some existing(and out of my control) authentication libraries(for example keycloak-angular ). The authentication libraries I'm wrapping need to get initialized with configuration parameters during app initialization or bootstrapping which works when hard coding the configurations inside my Wrapper library but I can't get it to work with injection or forRoot from a sample application for my wrapper library.

How to pass the configuration parameters from my sample application through my wrapper library to the authentication libraries?


Solution

  • You can pass the configuration from App.Module using forRoot to your Library Module.

    Try like this:

    App.module.ts

     imports: [
          BrowserModule,
          AuthLibModule.forRoot({
            configuration: {configA : "abc", configB : "xyz"}
          })
       ]
    

    AuthLibModule.module.ts

    export class AuthLibModule{ 
      static forRoot(configuration): ModuleWithProviders {
        console.log(configuration);
        return {
          ngModule: AuthLibModule,
          providers: [AuthLibService,{provide: 'config', useValue: configuration}]
        };
      }
    }
    

    AuthLibService.service :

    export class AuthLibService{
    
      constructor(@Inject('config') private config:any) {
        console.log(config)
       }
    }