使用Angular访问模块文件中配置文件中的动态值



下面是我的config-dev的一部分。json文件

"authConfig": {     
"stsServer": "https://xxx-dev.xxx.com/cls/security/authorization/v2",
"redirectUrl": "https://xxx-dev.xxx.com/grids/security/authCallback",
"clientId": "xxxxxx",      
}, 

我正在为所有环境(如config-dev)在配置文件中设置认证相关配置。json, config-qa。json等。

所以,在我使用的包(angular-auth-oidc-client)的最新更改之后,我需要在我的代码中进行必要的更改。

下面是auth-config.模块的一部分。ts文件

export const authValues: OpenIdConfiguration | OpenIdConfiguration[] = {
"stsServer": "https://xxx-dev.xxx.com/cls/security/authorization/v2",
"redirectUrl": "https://xxx-dev.cscglobal.com/grids/security/authCallback",
"clientId": "xxxxxx",      
}
@NgModule({
imports: [AuthModule.forRoot({config: authValues})],    
exports: [AuthModule],
})
现在,我需要在forRoot方法下提供配置值。在这里,我无法理解的是我如何从各自环境的配置文件动态地获得值?通过像上面的例子一样在常量中手动给出它们,我只能为一个环境设置。

对于我的应用程序中的其他场景,我过去常常通过在构造函数中提供AppConfig文件,然后通过访问这些值来获取动态值。

app.config.ts

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
@Injectable()
export class AppConfig {
public config: any;
constructor(private httpClient: HttpClient) {}
public load() {
return new Promise((resolve, reject) => {
this.httpClient.get('/conf/config.json')
.subscribe((configData) => {
this.config = configData;
resolve(true);
}, (error) => {
console.log(error);
});
});
}
} 

在任何组件或服务类中加载此构造函数后,我可以通过执行

来访问配置文件的值
constructor(private appConfig: AppConfig)

然后

this.appConfig.config.authConfig.stsServer

但是通过使用上述方法,我无法获得模块文件中的值。有没有别的方法可以让我得到这些动态的值。请建议。谢谢。

只要设置文件替换,Angular就会根据你运行的环境确保你有正确的文件。

最新更新