environment是什么?
我們?cè)谟胊ngular-cli創(chuàng)建angular項(xiàng)目時(shí)凌那,會(huì)默認(rèn)生成一個(gè)environment文件
里面會(huì)有兩個(gè)文件
environment.ts
// This file can be replaced during build by using the `fileReplacements` array.
// `ng build --prod` replaces `environment.ts` with `environment.prod.ts`.
// The list of file replacements can be found in `angular.json`.
//在生成過(guò)程中胧沫,可以使用“fileReplacements”數(shù)組替換此文件昌简。
//“ng build--prod”將“environment.ts”替換為“environment.prod.ts”。
//文件替換列表可以在“angular.json”中找到绒怨。
export const environment = {
production: false,
baseUrl:'http://dev.test.com'
};
/*
* For easier debugging in development mode, you can import the following file
* to ignore zone related error stack frames such as `zone.run`, `zoneDelegate.invokeTask`.
*
* This import should be commented out in production mode because it will have a negative impact
* on performance if an error is thrown.
*/
// import 'zone.js/dist/zone-error'; // Included with Angular CLI.
/*
*為了便于在開發(fā)模式下調(diào)試纯赎,可以導(dǎo)入以下文件
*忽略與區(qū)域相關(guān)的錯(cuò)誤堆棧幀,如“zone.run”窖逗、“zoneDelegate.invokeTask”址否。
*這個(gè)導(dǎo)入應(yīng)該在生產(chǎn)模式中被注釋掉餐蔬,因?yàn)樗鼤?huì)產(chǎn)生負(fù)面影響
*如果拋出錯(cuò)誤碎紊,將影響性能。
*/
//導(dǎo)入'zone.js/dist/zone error'樊诺;//包括在Angular CLI中仗考。
environment.prod.ts
export const environment = {
production: true,
baseUrl: 'http://prod.test.com'
};
這兩個(gè)文件都有一個(gè)production參數(shù),為是否是生產(chǎn)環(huán)境词爬,想到這里就一目了然秃嗜,我們打包后,生效的肯定是environment.prod.ts文件配置,值為true,實(shí)際我們?cè)诒镜卣{(diào)試時(shí)锅锨,生效的是environment.ts文件叽赊,打包后會(huì)去自動(dòng)替換成environment.prod.ts,其中的baseUrl參數(shù)是我們自定義添加的url地址必搞,針對(duì)不同環(huán)境會(huì)自動(dòng)調(diào)用對(duì)應(yīng)的地址必指,我們本地環(huán)境中調(diào)試可以調(diào)用一下這個(gè)environmentts
import {environment} from '../../environments/environment'
@Injectable()
export class InterceptorInterceptor implements HttpInterceptor {
baseUrl = environment.baseUrl;
constructor(private $commom: CommonService) {
console.log(this.baseUrl);
}
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
return next.handle(req).pipe(
tap((event: any) => {
if (event instanceof HttpResponseBase) this.handleData(event);
return of(event)
}),
catchError((err: HttpErrorResponse) => this.handleData(err))
)
}
private handleData(res: HttpResponseBase): Observable<any> {
console.log(res);
if (res instanceof HttpResponse) {
let body = (res as any).body;
if (body.code != 200) {
this.$commom.error(body.message);
}
if (body.code === 1003) {
/**todo => jump()*/
}
}
if (res instanceof HttpErrorResponse) {
this.$commom.error(res.error);
}
return of(res)
}
}
啟動(dòng)服務(wù)后,我們請(qǐng)求下接口恕洲,發(fā)現(xiàn)默認(rèn)的地址是http://dev.test.com
我們需要打包塔橡,然后再看看這個(gè)baseUrl打印的是什么,打包的時(shí)候需要輸入 ng build --prod -c production
我是在express中開啟了一個(gè)node服務(wù)霜第,把剛才打的靜態(tài)包丟在靜態(tài)目錄下就可以葛家,訪問(wèn)這個(gè)包的資源
訪問(wèn)后我們看到的baseUrl的地址變成了http://prod.test.com
雖然前后我們并沒有去改變environment.ts的路徑,我們?cè)L問(wèn)的一直是environment文件的baseUrl泌类,但為什么打包后會(huì)變成environment.prod.ts文件的baseUrl呢癞谒,說(shuō)明我們的打包命令 ng build --prod -c production會(huì)給我們自動(dòng)切換,會(huì)自動(dòng)替換environment的內(nèi)容刃榨,通過(guò)查找angular.json的配置我們不難發(fā)現(xiàn)如下的參數(shù)配置
也就是說(shuō)扯俱,當(dāng)我們啟動(dòng) -c production ,這條配置就生效了喇澡,很好理解的是replace替換的字段迅栅,說(shuō)明者里才是核心配置,那么問(wèn)題來(lái)了晴玖,我有多個(gè)環(huán)境的情況下該如何去配置環(huán)境變量嗯读存?
多環(huán)境配置baseUrl
多環(huán)境的情況下,我們有多個(gè)url呕屎,讓其自動(dòng)切換让簿,此時(shí)假如我還有個(gè)測(cè)試環(huán)境,那就需要再建一個(gè)environment.test.ts文件
重點(diǎn)來(lái)了,還是在我們的angular.json文件中配置
我們需要復(fù)制一份production秀睛,然后把production改成test,再把fileReplacements中的替換地址換一下尔当,換成是environment.test.ts,再把production改成是test
此時(shí)假如我們要發(fā)測(cè)試環(huán)境蹂安,在終端中輸入 ng build --prod -c=test,也可以直接配置package.json文件椭迎, 啟動(dòng)npm run build
假如我們需要配置生產(chǎn)環(huán)境production,在終端輸入 ng build --prod -c=production ,同樣可以配置pabckage.json文件
我們打包測(cè)試環(huán)境看下baseUrl打印出來(lái)是什么樣的,打包后的文件丟到本地express開啟服務(wù)的靜態(tài)目錄里訪問(wèn)