我正在try 将Angular 5.2应用程序更新为Angular 6.我成功地遵循了Angular update guide(包括angular-cli
到v6的更新)中的说明,现在我正试图通过
ng serve --env=local
但这给了我一个错误:
未知选项:‘--env’
我使用多个环境(dev/local/prod
),这是Angular 5.2中的工作方式.现在如何在Angular 6中设置环境?
我正在try 将Angular 5.2应用程序更新为Angular 6.我成功地遵循了Angular update guide(包括angular-cli
到v6的更新)中的说明,现在我正试图通过
ng serve --env=local
但这给了我一个错误:
未知选项:‘--env’
我使用多个环境(dev/local/prod
),这是Angular 5.2中的工作方式.现在如何在Angular 6中设置环境?
你需要使用新的configuration
选项(这也适用于ng build
和ng serve
)
ng serve --configuration=local
或
ng serve -c local
If you look at your angular.json
file, you'll see that you have finer control over settings f或 each configuration (aot, optimizer, environment files,...)
"configurations": {
"production": {
"optimization": true,
"outputHashing": "all",
"sourceMap": false,
"extractCss": true,
"namedChunks": false,
"aot": true,
"extractLicenses": true,
"vend或Chunk": false,
"buildOptimizer": true,
"fileReplacements": [
{
"replace": "src/environments/environment.ts",
"with": "src/environments/environment.prod.ts"
}
]
}
}
You can get m或e info here f或 managing environment specific configurations.
正如下面的另一个响应中指出的,如果需要添加新的"环境",则需要向build任务添加新配置,并根据需要向serve和test任务添加新配置.
Adding a new environment个
Edit:
为清楚起见,必须在build
部分中指定文件替换.因此,如果您希望将ng serve
与特定的environment
文件(例如dev2)一起使用,则首先需要修改build
部分以添加新的dev2配置
"build": {
"configurations": {
"dev2": {
"fileReplacements": [
{
"replace": "src/environments/environment.ts",
"with": "src/environments/environment.dev2.ts"
}
/* You can add all other options here, such as aot, optimization, ... */
],
"serviceW或ker": true
},
然后修改serve
部分以添加新配置,指向刚才声明的dev2 build
配置
"serve":
"configurations": {
"dev2": {
"browserTarget": "projectName:build:dev2"
}
然后您可以使用ng serve -c dev2
,它将使用dev2配置文件