我是angular-cli的新手,希望通過env爲我的api服務調用加載url。例如。angular-cli for angular2如何加載環境變量
local: http://127.0.0.1:5000
dev: http://123.123.123.123:80
prod: https://123.123.123.123:443
例如,在environment.prod.ts
我想這:
export const environment = {
production: true
"API_URL": "prod: https://123.123.123.123:443"
};
但從angular2,我該怎麼稱呼,所以我可以API_URL?
例如
this.http.post(API_URL + '/auth', body, { headers: contentHeaders })
.subscribe(
response => {
console.log(response.json().access_token);
localStorage.setItem('id_token', response.json().access_token);
this.router.navigate(['/dashboard']);
},
error => {
alert(error.text());
console.log(error.text());
}
);
}
感謝
但這種方式:從「../../environments/environment」進口{}環境; 你將你的應用程序硬編碼到開發環境。 prod環境文件稱爲environment.prod.ts。如果你做'ng build --prod',那麼ValuesService如何從environtment.prod獲取url? – Pascal
構建時,將爲您指定的環境打包相應的環境文件。 –