2017-04-06 50 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)