如何在Angular 6中通过`ng serve`设置环境 [英] How to set environment via `ng serve` in Angular 6
问题描述
我正在尝试将Angular 5.2应用程序更新为Angular6.我成功地遵循了Angular更新指南中的说明(包括将angular-cli
更新到v6),现在我正尝试通过
I am trying to update my Angular 5.2 app to Angular 6. I successfully followed instructions in the Angular update guide (including the update of angular-cli
to v6), and now I am trying to serve the app via
ng serve --env=local
但这给了我错误:
未知选项:-env"
Unknown option: '--env'
我使用多个环境(dev/local/prod
),这就是它在Angular 5.2中的工作方式.我现在如何在Angular 6中设置环境?
I use multiple environments (dev/local/prod
), and this is the way it was working in Angular 5.2. How can I set the environment now in Angular 6?
推荐答案
您需要使用新的configuration
选项(这也适用于ng build
和ng serve
)
You need to use the new configuration
option (this works for ng build
and ng serve
as well)
ng serve --configuration=local
或
ng serve -c local
如果查看您的angular.json
文件,您会发现您可以更好地控制每个配置的设置(自动配置,优化程序,环境文件...)
If you look at your angular.json
file, you'll see that you have finer control over settings for each configuration (aot, optimizer, environment files,...)
"configurations": {
"production": {
"optimization": true,
"outputHashing": "all",
"sourceMap": false,
"extractCss": true,
"namedChunks": false,
"aot": true,
"extractLicenses": true,
"vendorChunk": false,
"buildOptimizer": true,
"fileReplacements": [
{
"replace": "src/environments/environment.ts",
"with": "src/environments/environment.prod.ts"
}
]
}
}
您可以在此处获取更多信息,以管理特定于环境的信息配置.
You can get more info here for managing environment specific configurations.
如以下其他答复所述,如果您需要添加新的环境",则需要向
As pointed in the other response below, if you need to add a new 'environment', you need to add a new configuration to the build task and, depending on your needs, to the serve and test tasks as well.
添加新环境
修改:
为了清楚起见,必须在build
部分中指定文件替换.因此,如果要对特定的environment
文件(例如 dev2 )使用ng serve
,则首先需要修改build
部分以添加新的 dev2 配置
Edit:
To make it clear, file replacements must be specified in the build
section. So if you want to use ng serve
with a specific environment
file (say dev2), you first need to modify the build
section to add a new dev2 configuration
"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, ... */
],
"serviceWorker": true
},
然后修改您的serve
部分,以添加一个新配置,指向您刚刚声明的 dev2 build
配置
Then modify your serve
section to add a new configuration as well, pointing to the dev2 build
configuration you just declared
"serve":
"configurations": {
"dev2": {
"browserTarget": "projectName:build:dev2"
}
然后您可以使用ng serve -c dev2
,它将使用dev2配置文件
Then you can use ng serve -c dev2
, which will use the dev2 config file
这篇关于如何在Angular 6中通过`ng serve`设置环境的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!