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

But this gives me error:

Unknown option: '--env'

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?

推荐答案

You need to use the new configuration option (this works for ng build and ng serve as well)

ng serve --configuration=local

or

ng serve -c local

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.

Adding a new environment

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
        },

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"
        }

Then you can use ng serve -c dev2, which will use the dev2 config file

Angular相关问答推荐

如何go 除融合图中的拖尾水印?

为什么当我在父组件中设置数组元素时,Angular重新创建子组件而不是更新它?

如何跨不同组件使用ng模板

尾风手风琴中的Ngor

如何在投影项目组周围添加包装元素?

基于另一个控制值显示值的Angular 数组

笔刷Angular 为17

以Angular 动态添加和删除组件形状元素

如何从URL中角下载图片?

带独立零部件的TranslateLoader[Angular 16]

bootstrap 可折叠菜单在单击时未展开.Angular 应用程序

Angular将文件作为字符串读取给dxf解析器

如果我使用 matAutocomplete,为什么 textarea 不显示 formControl 的实际值

Angular 14 类型表单 - 数字控件的初始值

Angular 2 中的 ChangeDetectionStrategy.OnPush 和 Observable.subscribe

angular2:错误: TypeError: Cannot read property '...' of undefined

等待多个promises完成

如何在Angular 4中为数字管道指定语言环境千位分隔符

Angular Material 模态对话框周围的空白

BrowserAnimationsModule 和 NoopAnimationsModule 有什么区别?