天天看點

Angular6 配置項目開發環境,測試環境,生産環境

搜過很多文章說的都是關于Angular4的配置,後面自己又找了許多資料,最後總結了Angular6 如何配置開發環境,測試環境,生産環境:

1、在environments檔案夾裡建立三個檔案:

//生産環境
environment.prod.ts:
export const environment = {
  production: true,
  url  : 'http://xxx',
  url2 : 'http://xxx',
  url3 : 'http://xxx',
};
//測試環境
environment.test.ts:
export const environment = {
  production: false,
  url  : 'http://xxx',
  url2 : 'http://xxx',
  url3 : 'http://xxx'
};
//本地環境
environment.ts:
export const environment = {
  production: false,
  url  : 'http://xxx',
  url2 : 'http://xxx',
  url3 : 'http://xxx',
};
           

2、重點是第二步(有别于Angular4):

找到angular.json檔案

找到 projects - architect - build - 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"
                }
              ]
            },
            "test": {
              "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.test.ts"
                }
              ]
            }
           

3、在接口服務裡引入

import {environment} from '../environments/environment';
   url = environment.url;
   url2 = environment.url2;
   url3 = environment.url3;
           

4、package.json 配置裡修改:

"scripts": {
    "ng": "ng",
    "start": "ng serve --host 192.168.1.187",
    "build": "ng build --prod --configuration=production",
    "buildTest": "ng build --prod --configuration=test",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e"
  },
           

繼續閱讀