我正在为一些现有的(并且不受我控制的)身份验证库(例如keycloak-angular)构建包装器库auth-lib。 我包装的身份验证库需要在应用程序初始化或引导过程中使用配置参数进行初始化,这在对Wrapper库中的配置进行硬编码时可以使用,但是我无法从包装的示例应用程序中将其与注入或forRoot一起使用库。
如何将示例应用程序中的配置参数通过包装器库传递到身份验证库?
我正在为一些现有的(并且不受我控制的)身份验证库(例如keycloak-angular)构建包装器库auth-lib。 我包装的身份验证库需要在应用程序初始化或引导过程中使用配置参数进行初始化,这在对Wrapper库中的配置进行硬编码时可以使用,但是我无法从包装的示例应用程序中将其与注入或forRoot一起使用库。
如何将示例应用程序中的配置参数通过包装器库传递到身份验证库?
您可以使用forRoot
将配置从App.Module传递到库模块。
尝试这样:
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)
}
}