Angular6:多种配置(twas环境)

Angular 6: Multiple configurations (twas environments)

正在尝试让 angular-cli 识别 angular.json

中的多个配置
C:\_dev\myapp>ng serve --configuration development
Configuration 'development' could not be found in project 'myapp'.
Error: Configuration 'development' could not be found in project 'myapp'.

代码段是:

    "configurations": {
        "production": {
          "fileReplacements": [
            {
              "replace": "src/environments/environment.ts",
              "with": "src/environments/environment.production.ts"
            }
          ],
          "optimization": true,
          "outputHashing": "all",
          "sourceMap": false,
          "extractCss": true,
          "namedChunks": false,
          "aot": true,
          "extractLicenses": true,
          "vendorChunk": false,
          "buildOptimizer": true
        },
        "development": {
          "fileReplacements": [
            {
              "replace": "src/environments/environment.ts",
              "with": "src/environments/environment.development.ts"
            }
          ],
          "optimization": false,
          "outputHashing": "all",
          "sourceMap": true,
          "extractCss": true,
          "namedChunks": true,
          "aot": false,
          "extractLicenses": false,
          "vendorChunk": true,
          "buildOptimizer": false
        }
      }

src/environments/environment.development.ts确实存在

ng serve --configuration production 

工作正常

buildangular.json 文件的 serve 部分中有一个 configurations 条目。服务部分也需要了解您的自定义配置。假设你的配置名称是debug,添加到serve部分如下

"projects": {
  "myApp": {
     [...]
     "architect": {
       "build": {
         [...]
         "configurations": {
           "production": { [...] },
           "debug": { [...] }
         }
       },
       "serve": {
         [...]
         "configurations": {
           "production": {
             "browserTarget": "myApp:build:production"
           },
           "debug": {
             "browserTarget": "myApp:build:debug"
           }
         }
       }
     }
   }
 }

不要忘记将 myApp 调整为等于 angular.jsonproject 部分的直接子项的项目名称.此外,debug 都应与您在 build 部分中的配置相匹配。

然后上菜

ng serve --configuration=debug

对于 Angular 2 - 5 请参阅文章以了解使用 Multiple Environment in angular

的分步解决方案

对于 Angular 6 使用 ng serve --configuration=dev

Note: Refer the same article for angular 6 as well. But wherever you find --env instead use --configuration. That's works well for angular 6.