项目在开发时与发布后的配置文件(比如url)是有区别的,因此需要根据不同的场景使用不同的配置。
环境
>ng version
_ _ ____ _ ___
/ \ _ __ __ _ _ _| | __ _ _ __ / ___| | |_ _|
/ △ \ | '_ \ / _` | | | | |/ _` | '__| | | | | | |
/ ___ \| | | | (_| | |_| | | (_| | | | |___| |___ | |
/_/ \_\_| |_|\__, |\__,_|_|\__,_|_| \____|_____|___|
|___/
Angular CLI: 7.3.3
Node: 10.13.0
OS: win32 x64
Angular: 7.2.6
... animations, common, compiler, compiler-cli, core, forms
... language-service, platform-browser, platform-browser-dynamic
... router
Package Version
-----------------------------------------------------------
@angular-devkit/architect 0.13.3
@angular-devkit/build-angular 0.13.3
@angular-devkit/build-optimizer 0.13.3
@angular-devkit/build-webpack 0.13.3
@angular-devkit/core 7.3.3
@angular-devkit/schematics 7.3.3
@angular/cli 7.3.3
@ngtools/webpack 7.3.3
@schematics/angular 7.3.3
@schematics/update 0.13.3
rxjs 6.3.3
typescript 3.2.4
webpack 4.29.0
思路
分别配置两套 Config,dev、prod,之后在发布网站时选择不同的配置项进行发布即可。
配置
打开 angular.json -> projects -> 项目名 -> architect -> build -> configurations -> production
。复制该配置以设置更多的配置项,如下:
"configurations": {
"prod": {
"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,
"budgets": [
{
"type": "initial",
"maximumWarning": "2mb",
"maximumError": "5mb"
}
]
},
"dev": {
"fileReplacements": [
{
"replace": "src/environments/environment.ts",
"with": "src/environments/environment.dev.ts"
}
],
"optimization": true,
"outputHashing": "all",
"sourceMap": false,
"extractCss": true,
"namedChunks": false,
"aot": true,
"extractLicenses": true,
"vendorChunk": false,
"buildOptimizer": true,
"budgets": [
{
"type": "initial",
"maximumWarning": "2mb",
"maximumError": "5mb"
}
]
}
}
上面的配置中,配置了 2 个配置项,分别是 dev 与 prod。
dev 与 prod 是配置中节点的名字,随后 build 时需要用到他们。
编辑 dev 与 prod 环境文件
在 src -> environments
中复制 environments.prod.ts
, 粘贴后更改名字为 environments.dev.ts
。
environment.ts 文件内容:
export const environment = {
production: false,
envType: 'test'
};
environment.dev.ts 文件内容:
export const environment = {
production: false,
envType: 'dev'
};
environment.prod.ts 文件内容:
export const environment = {
production: true,
envType: 'prod'
};
此时的 environments 文件夹应该是这样的
├── environment.dev.ts
├── environment.prod.ts
└── environment.ts
使用配置项
app.component.html:
<h2>{{envType}}</h2>
app.component.ts:
import { Component, OnInit } from '@angular/core';
import { environment } from 'src/environments/environment';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss']
})
export class AppComponent implements OnInit {
public envType: string;
constructor() { }
ngOnInit(): void {
this.envType = environment.envType;
}
}
为了方便测试,打开 angular.json -> projects -> 项目名 -> architect -> serve -> configurations
修改如下:
"configurations": {
"prod": {
"browserTarget": "ng-practice:build:prod"
},
"dev": {
"browserTarget": "ng-practice:build:dev"
}
}
分别执行: ng s -c=dev
与 ng s -c=prod
与 ng s
,会看到不同的结果:dev
, prod
, test
。
在发布时,需要将命令稍作修改: ng b --prod -c=prod
。