简体   繁体   English

如何构建具有不同环境的角度项目

[英]how to build angular project with different environment

I am using Angular 5.2 and I need to bundle like it does with ng build --prod but with the different environment我正在使用Angular 5.2 ,我需要像使用ng build --prod那样进行捆绑,但使用不同的环境

I tried:我试过:

ng build --env=qa --aot --vendorChunk --common-chunk --output-hashing=bundles

but it does not give me the same bundling as i see with --prod但它并没有给我提供与 --prod 相同的捆绑

it generates both a .js and .js.map file它同时生成 .js 和 .js.map 文件

main.66dc6fba707fe2f3314c.bundle.js
main.66dc6fba707fe2f3314c.bundle.js.map

What options should I use to get me the same result at --prod but with a different environment?我应该使用哪些选项来在 --prod 处获得相同的结果但使用不同的环境?

In angular 6 you can create multiple environments in angular.json在 angular 6 中,您可以在 angular.json 中创建多个环境

Find configuration and inside that you can create multiple environment with various settings which you can find from here https://github.com/angular/angular-cli/wiki/angular-cli查找配置并在其中创建具有各种设置的多个环境,您可以从这里找到https://github.com/angular/angular-cli/wiki/angular-cli

example例子

"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
  },
  "staging": {
    "fileReplacements": [
      {
        "replace": "src/environments/environment.ts",
        "with": "src/environments/environment.staging.ts"
      }
    ],
    "optimization": true,
    "outputHashing": "all",
    "sourceMap": false,
    "extractCss": true,
    "namedChunks": false,
    "aot": true,
    "extractLicenses": true,
    "vendorChunk": false,
    "buildOptimizer": true
  }
}

As you can see i have created an another environment name staging如您所见,我创建了另一个环境名称暂存

The Dummy angular.json file is https://api.myjson.com/bins/12k70w虚拟 angular.json 文件是https://api.myjson.com/bins/12k70w

To run the application with specific environment just use要在特定环境下运行应用程序,只需使用

ng build --configuration=staging

I have also created a file in environment called environment.staging.ts我还在名为 environment.staging.ts 的环境中创建了一个文件

export const environment = {
    production: true,
    APIEndpoint: "http://0.0.0.0:8080/api"
};

When you are creating environment file, we need to set production: true which will enable Production Build with AOT by default in main.ts.在创建环境文件时,我们需要设置 production: true ,这将在 main.ts 中默认启用 Production Build with AOT。

environment.stage.ts环境.stage.ts

export const environment = {
  production: true
};

main.ts主文件

if (environment.production) {
  enableProdMode();
}


Cmd: ng build --prod --env=stage

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM