【问题标题】:How to specify environment via `ng build` in Angular 6 app如何在Angular 6应用程序中通过`ng build`指定环境
【发布时间】:2018-10-20 07:52:21
【问题描述】:

在 Angular 5 中, 我们可以使用

为不同的环境生成构建
ng build --prod --env=uat

迁移到 Angular 6 后,上述命令抛出错误

Unknown option: '--env'

【问题讨论】:

    标签: angular angular-cli angular6


    【解决方案1】:

    需要使用配置选项

    ng build --prod --configuration=uat
    

    ng build --prod -c uat
    

    更多信息here

    同样对于 ng 提供与回答 here 相同的选项

    【讨论】:

    • 为什么在血腥的 H 中他们会从 ng build --help 中删除该信息。感谢@nehal 的分析器
    • angular7 它的ng build --prod --aot
    • 至少在 Angular 8 中,没有理由同时指定 --prod--configuration--configuration 覆盖 --prodSource
    • ng build --prod --configuration=uat 正是我想要的。非常感谢!
    【解决方案2】:

    你可以尝试使用

    ng build ---prod

    【讨论】:

    • OP 希望为特定环境设置构建,因此需要以某种方式指定。 --prod 是主要的“发布”构建,它执行捆绑和类似的东西,但环境本身可以有配置被放入
    【解决方案3】:

    我已经在 Angular 6 项目中进行了测试。

    ng build --prod --configuration=uat 似乎不起作用,因为它仅在您运行此命令时选择 uat 配置并忽略 --prod 标志并且不应用任何优化,例如 aot、缩小和升级等。

    运行ng build --prod --configuration=uat 与只运行ng build --configuration=uat 的效果相同。为了应用任何其他配置选项,我们需要在 angular.json 的 uat 构建选项中显式添加它们

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

    【讨论】:

      【解决方案4】:

      你可以尝试使用:

      ng build --configuration=uat
      

      【讨论】:

        【解决方案5】:
        Prod: ng build --prod
        Qa: ng build --configuration=qa
        
        angular.json
        "production": {
                      "fileReplacements": [
                        {
                          "replace": "src/environments/environment.ts",
                          "with": "src/environments/environment.prod.ts"
                        }
                      ]
        },
        "qa": {
                      "fileReplacements": [
                        {
                          "replace": "src/environments/environment.ts",
                          "with": "src/environments/environment.qa.ts"
                        }
                      ]
        }
        
        PROD:
        export const environment = {
          production: true,
          api : 'https://example.com'
        }
        
        QA:
        export const environment = {
            production: true,
            api : 'https://example-Qa.com'
        }
        
        dev environment
        export const environment = {
            production:false,
            api : 'https://example-dev.com'
        }
        

        【讨论】:

          猜你喜欢
          • 2018-10-14
          • 2020-10-03
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-10-17
          • 1970-01-01
          • 1970-01-01
          • 2020-09-23
          相关资源
          最近更新 更多