Angular Workspace 配置指南的构建配置部分讨论了如何在同一个应用程序中管理各种配置。
就我而言,我决定为每个客户创建一个配置:
// angular.json excerpt
"architect": {
"build": {
"configurations": {
"customer-1": {
"fileReplacements": [
{
"replace": "src/environments/environment.ts",
"with": "src/environments/environment.customer-1.ts"
}
]
},
"customer-2": {
"fileReplacements": [
{
"replace": "src/environments/environment.ts",
"with": "src/environments/environment.customer-2.ts"
}
]
},
}
},
"serve": {
"builder": "@angular-devkit/build-angular:dev-server",
"configurations": {
"customer-1": {
"browserTarget": "my-app:build:customer-1"
},
"customer-2": {
"browserTarget": "my-app:build:customer-2"
},
}
我使用环境文件来管理各种客户特定的设置和production
标志:
// environment.customer-1.ts excerpt
const environment = {
production: true,
customer: "Customer 1",
};
// environment.customer-2.ts excerpt
const environment = {
production: true,
customer: "Customer 2",
};
最后,我需要一种方法来确定应用程序是否是使用ng serve
或ng build
命令启动的,以便在应用程序模块中导入一些模拟的提供程序。
我尝试使用该environment.production
标志,但显然它总是正确的:
import { environment } from '../environments/environment';
@NgModule({
providers: [
environment.production // is always true since I've set the flag in all customer-specific environment.ts files
? []
: nonProductionProviders,
],
bootstrap: [AppComponent]
})
export class AppModule { }
我想出支持这种情况的唯一方法是创建单独的environment.ts
文件以及匹配的配置,例如
/src/environment.customer-1-dev.ts
/src/environment.customer-1-prod.ts
// angular.json excerpt
"architect": {
"build": {
"configurations": {
"customer-1-dev": {
"fileReplacements": [
{
"replace": "src/environments/environment.ts",
"with": "src/environments/environment.customer-1-dev.ts"
}
]
},
// etc.
有一个更好的方法吗?传递一个额外的标志或至少扩展配置的能力会很好。