Angular 使用多个环境文件
在Angular 7应用程序上,我有以下Angular 使用多个环境文件,angular,angular7,Angular,Angular7,在Angular 7应用程序上,我有以下environment.prod.ts文件: export const environment = { production: true, apiBaseUri: 'https://api.xyz.com' }; 我需要在两台不同的机器上发布应用程序 对于每一种,我需要两种配置:生产和暂存 因此,我需要类似以下环境文件的内容: environment.prod.machine-1.ts environment.prod.machine-2.ts
environment.prod.ts
文件:
export const environment = {
production: true,
apiBaseUri: 'https://api.xyz.com'
};
我需要在两台不同的机器上发布应用程序
对于每一种,我需要两种配置:生产和暂存
因此,我需要类似以下环境文件的内容:
environment.prod.machine-1.ts
environment.prod.machine-2.ts
environment.staging.machine-1.ts
environment.staging.machine-2.ts
我应该如何定义这些环境文件
以及如何构建应用程序?在
angular.json
中,在的“projects..architect.build.configurations”下,您可以定义每台机器的每种配置类型。在每个配置下定义fileReplacements
,如下所示:
"projects": {
"<your project name>": {
"architect": {
"build": {
"configurations": {
"machine-1.production": {
"fileReplacements": [
{
"replace": "src/environments/environment.ts",
"with": "src/environments/environment.prod.machine-1.ts"
}
]
},
"machine-1.staging": {
"fileReplacements": [
{
"replace": "src/environments/environment.ts",
"with": "src/environments/environment.staging.machine-1.ts"
}
]
},
// same for machine 2, etc...
}
}
}
}
}
(或您想要的任何其他环境)。我看到的可能的副本。让我困惑的一件事是在环境文件中使用“production:true/false”。它做什么?@MiguelMoura根据angular.json
模式,在“配置”下没有定义这样的“生产”
属性。你到底是什么意思?您是否引用了ng build
的--prod
参数?如果是这样的话,它只是--配置=生产
的一个快捷方式,因此如果在“projects..architect.build.configurations”
下没有名为“production”
的配置,运行ng build--prod将无法工作。