在Angular2应用程序中使用Docker环境variables

我想在Angular-Cli(1.0.0-beta.16)的'environment.prod.ts'中使用预定义的Docker Environmentvariables。

用例:

environment.prod.ts:

export const environment = { production: true, host1: $ENV1, //specific service endpoint goes here host2: $ENV2 //specific service endpoint goes here }; 

我的Angular基于CLI的应用程序依赖于environment.prod.ts来获取有关后端服务(host1,host2)和应用程序的主机信息经历了DEV,TEST,QA,PROD等各种环境…

在Docker运行期间提供$ ENV1,$ ENV2作为环境variables

这可能吗 ? 如果没有,请纠正我的理解和build议任何其他的替代解决scheme

angular度cli环境是编译时间常量。 这当然有点矛盾,您在使用Docker时如何考虑应用程序。 使用Docker,您希望在不同的环境中运行相同的工件(使用环境variables)。

到现在为止,我提出了两个解决scheme,虽然对我来说都觉得很不方便:

使用服务器端渲染

…并将variables注入全局范围。 你可以在开始的<html>标签之后添加另外一个带有所需variables的<script> <html>标签:

 <script> var environment = { host1: 'some injected value', host2: 'depending on the view engine you are using' }; </script> 

然后有一个接口

 export interface Environment { host1: string; host2: string; } interface Window { environment: Environment; } 

使用API​​调用

…在应用程序启动时/之后获取variables

您可以在onInit()中的app.component.ts中执行此操作,或者在您的第一个path上设置警戒 。