我正在为一些现有的(并且不受我控制的)身份验证库(例如keycloak-angular)构建一个包装库“auth-lib”。我包装的身份验证库需要在应用程序初始化或引导过程中使用配置参数进行初始化图书馆。
如何通过我的包装库将配置参数从我的示例应用程序传递到身份验证库?
我正在为一些现有的(并且不受我控制的)身份验证库(例如keycloak-angular)构建一个包装库“auth-lib”。我包装的身份验证库需要在应用程序初始化或引导过程中使用配置参数进行初始化图书馆。
如何通过我的包装库将配置参数从我的示例应用程序传递到身份验证库?
您可以将配置从 App.Module 传递forRoot
给您的库模块。
试试这样:
应用模块.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)
}
}