Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 553
Mohamed Talaat Harb Avatar asked Dec 26 '19 06:12

Mohamed Talaat Harb


1 Answers

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)
   }
}
like image 53
Adrita Sharma Avatar answered Oct 31 '22 12:10

Adrita Sharma