Angular 2构建一次,随处部署

时间:2017-06-09 08:41:02

标签: angular tomcat docker deployment

是否有可能遵循“构建一次,在任何地方部署”的原则来构建angular 2个应用程序?

我一直在找到答案,告诉我要为每个环境单独构建它,但这不是我想要的。

我们正在使用dockertomcat

提前致谢。

1 个答案:

答案 0 :(得分:1)

我认为加载动态配置的所有app的一个共同点就是使用名为var input = { 10: 1, 11: 1, 13: 1, 15: 1, 20: 1, 21: 2, 22: 2, 25: 2, 28: 3, 32: 1, 33: 3, 37: 1, 38: 1, 39: 2, 41: 1, 45: 2 }; function createKV(kv, isKey) { var div = document.createElement("div"); div.style.width = "100px"; div.style.height = "100px"; var color = isKey ? "green" : "blue"; div.style.background = color; div.style.color = "white"; div.innerHTML = kv; var placing = isKey ? "keys" : "values"; document.getElementById(placing).appendChild(div); } for (var key in input) { console.log(key); createKV(key, true); console.log(input[key]); createKV(input[key], false); } 的角度提供程序。

此提供程序允许在引导角度应用程序之前执行一些代码。通常,要执行的代码类似于APP_INITIALIZER。示例here

您可以自己编写代码,也可以使用此库(使用get some config from an endpoint, eg /configuration):https://github.com/ngx-config/core,更准确地说是APP_INITIALIZER

在下面的示例中,我们使用lib通过http调用加载 / configuration 。这可以在您的 app.module.ts

中找到
ConfigHttpLoader

然后,您提供 / configuration 的方式取决于您为应用提供的服务。我在一个项目中所做的是:

  • 前端应用程序打包了所有配置(dev.json,integ.json,prod.json)
  • 传递给docker一个环境变量(dev或integ或prod)
  • 在docker镜像的Dockerfile中,根据环境创建一个符号链接到正确的配置文件,然后启动服务器(在我的例子中是nginx):

export function configFactory(http: Http): ConfigLoader { return new ConfigHttpLoader(http, '/configuration'); // FILE PATH || API ENDPOINT } @NgModule({ imports: [ ConfigModule.forRoot({ provide: ConfigLoader, useFactory: (configFactory), deps: [Http] }) ] })

这样您的前端应用程序始终会加载 / configuration ,但根据环境的不同,符号链接将指向不同的配置文件。

你可以使用这样的东西。

相关问题