如何将配置参数传递给角度库?

Moh*_*arb 5 configuration bootstrapping angular2-bootstrapping angular angular-library

我正在为一些现有的(不受我控制的)身份验证库(例如keycloak-angular)构建一个包装库“auth-lib” 。我正在包装的身份验证库需要在应用程序初始化或引导期间使用配置参数进行初始化,这在我的 Wrapper 库中硬编码配置时有效,但我无法从我的包装器的示例应用程序中使用注入或 forRoot图书馆。

如何将配置参数从我的示例应用程序通过我的包装库传递到身份验证库?

Adr*_*rma 8

您可以将 App.Module 中的配置传递forRoot给您的库模块。

像这样尝试:

应用模块.ts

 imports: [
      BrowserModule,
      AuthLibModule.forRoot({
        configuration: {configA : "abc", configB : "xyz"}
      })
   ]
Run Code Online (Sandbox Code Playgroud)

AuthLibModule.module.ts

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

AuthLibService.service :

export class AuthLibService{

  constructor(@Inject('config') private config:any) {
    console.log(config)
   }
}
Run Code Online (Sandbox Code Playgroud)