目前,我在Angular4中拥有三种不同的环境:
现在,在vsts构建管道中,我设置了多重配置,其中一个定义为调试和发布准备了工件:
在运行npm install和npm run(调试或发布)之前,我正在使用“ 替换令牌 ”任务替换每个调试和发布环境的变量,然后运行webpack打包用于调试或发布环境的文件。
我看到Release中有一个替换变量的选项,您可以在其中替换.json文件中的变量(例如appsettings.json):
但是问题是当webpack将源代码打包到一个bundle.js中时,我想我不能使用release来替换环境变量吗?我可以吗?
因此,我要实现的是将调试和发布版本分离。这很简单,我只是为调试和发布重新创建了单独的定义,其中我仅在每个环境中替换变量。第二阶段是实际上从构建管道中删除“替换令牌”任务,并使用“发布变量”部分替换在“发布”中设置的每个环境的变量。webpack在js中构建包后,Angular怎么可能?
考虑以下情形(Angular v7):
第二点是这里的关键,看起来像这样:
@NgModule({
imports: [
...
OAuthModule.forRoot({
resourceServer: {
allowedUrls: [API_SERVER_URL], // <== we need to set the value that we loaded from the external endpoint (JSON) here
sendAccessToken: true
}
}),
...
],
declarations: [AppComponent],
bootstrap: [AppComponent]
})
export class AppModule { }
Run Code Online (Sandbox Code Playgroud)
我已经尝试了以下内容:
OAuthModule.forRoot()
会在APP_INITIALIZER
下载外部配置JSON 之前触发。main.ts
进入角环境变量,然后引导中的AppModule。由于中的import { AppModule } from './app/app.module';
语句也无法正常工作main.ts
,这会导致AppModule在加载OAuthModule.forRoot()
外部配置之前加载并启动(此注释确认了此行为)。main.ts …
我APP_INITIALIZER
用来加载环境特定的变量。我需要在我的中使用这些变量authConfigFactory
,但是工厂会APP_INITIALIZER
在应用程序配置内部完成之前保持加载状态。
我正在使用这个库:https : //github.com/manfredsteyer/angular-oauth2-oidc
我想使用APP_CONFIG.authConfig.allowedUrls
我的auth config工厂内部的值。我如何确保它在认证工厂之前先设置配置。
我在工厂收到此错误: Cannot read property 'authConfig' of undefined
app.module.ts
:
providers: [
AppConfigService,
{
provide: APP_INITIALIZER,
useFactory: (config: AppConfigService) => () => config.load(),
multi: true,
deps: [AppConfigService]
},
{
provide: OAuthModuleConfig,
useFactory: authConfigFactory
}
]
Run Code Online (Sandbox Code Playgroud)
app.config.ts
:
export let APP_CONFIG: AppConfig;
@Injectable()
export class AppConfigService {
constructor(
private injector: Injector
) {}
config = null;
public load() {
const http = this.injector.get(HttpClient);
return http
.get('../assets/config/config.json')
.pipe( …
Run Code Online (Sandbox Code Playgroud)