2017-04-06 30 views
0

可以根據環境定義靜態變量嗎?Angular 2:根據環境定義靜態變量

喜歡的東西(此代碼是一個愚蠢的例子,但它擁有的想法):

if (environment.production) { 
    public static MY_VAR: string = 'A'; 
} else { 
    public static MY_VAR: string = 'B'; 
} 

感謝您的諮詢!

+2

'公共靜態MY_VAR = environment.production? 'A':'B'? –

+0

謝謝你的解決方案! –

回答

0

當我第一次啓動應用程序,然後根據dev,qa和uat url設置我的應用程序中的api端點時,我所做的是從window.location.host中讀取url。 如何做到這一點?

在main.ts文件

platformBrowserDynamic([{provide:'EndPoint',useValue:config.getEndPoint()}]) 

export class config{ 
public static getEndPoint(){ 
const host = window.location.host 
if(host) // your logic goes here{ 

} 
} 
} 

在服務注入像

constructor(@Inject('EndPoint') private endPoint:string)