如何将 Angular 应用程序嵌入到另一个应用程序中? [英] how to embed an angular app into another app?

查看:46
本文介绍了如何将 Angular 应用程序嵌入到另一个应用程序中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的团队开发了一个 angular 5 应用程序,该应用程序已经投入生产一段时间了,但我们最近的任务是让该应用程序在公司拥有的其他 3 个站点上运行.一个站点是使用 Angular6 构建的 SPA,另一个站点也是使用 Angular5 的 SPA,而另一个站点则使用一些较旧的库,例如 jQuery.

My team develop an angular 5 application that has been in production for a while, but we've been tasked recently with making the app work in other 3 sites the company owns. One site is a SPA built with Angular6, other is also a SPA but uses Angular5, while the other is using some older libraries such as jQuery.

管理层希望我们立即与 Angular5 SPA 集成,因此我们只是将整个应用程序导出为带有子路由的模块,并让其他应用程序进行引导.

Management wanted us to integrate with the Angular5 SPA right away so we just exported the whole application as a module with child routes and let the other application do the bootstrap.

但恐怕上述方法不适用于非角度站点.这也使两个应用程序紧密结合,因为主机"应用程序需要了解我们应用程序的所有依赖项,这不是一个微不足道的应用程序(我认为非常大)并安装它们,当两个应用程序需要不同版本的应用程序时,这会导致问题相同的依赖,更不用说我们在升级依赖或框架本身时需要同步.我认为将应用嵌入更多网站时,这种方法不会扩展.

But I'm afraid the above approach will not work for the non-angular site. This also tight couple both applications since the 'host' app needs to know about all dependencies of our application which is not a trivial app (I'd say is pretty big) and install them, this caused problems when both applications needed different versions of the same dependency, no to mention that we will need to sync when upgrading dependencies or the framework itself. I don't think this approach will scale when embedding the app into more sites.

对于更通用的实现,我的第一个想法是将我们的应用程序升级到 Angular 6 并创建一个带有自定义元素的 Web 组件,但我们需要支持不支持原生封装的 IE11 和 Edge,因此我们需要进行测试我们的应用程序在每个使用它的站点中,以确保它们不会破坏我们的样式,而且我不知道 Web 组件是否可以管理子路由.

My first idea for a more general implementation was to upgrade our app to Angular 6 and create a web component with a custom element, but we need to support IE11 and Edge which do not support native encapsulation, so we would need to test our app in every site where it is used, to make sure they are not breaking our styles, also I don't know whether a web component can manage child routes or not.

其他想法是使用 iframe,但我的问题是调整 iframe 大小以适应内容,以及如何从 iframe 内的常驻"应用程序在主机"应用程序中添加子路由.

Other idea is to use an iframe but my problem here is the iframe resizing to adapt to the content and how to add child routes in the 'host' app from the 'resident' app inside the iframe.

有没有更好的方法来实现我们需要做的事情?

Is there a better way to achieve what we need to do?

理想的解决方案应该允许我们的应用程序在多个站点中使用(每个站点都提供特定的配置),而我们不必知道使用我们的应用程序的站点.

The ideal solution should allow our application to be used in multiple sites (each one providing specific configuration) without us having to know about the site using our app.

感谢您的帮助.

推荐答案

UPDATE 2020

您还可以使用新的 Webpack 5 模块联合.

以下示例展示了如何将模块联合与 Angular 和其他技术结合使用.

following examples show how to use module federation with Angular and other technologies.

原答案

我最近一直忙于这个话题,因为很多人一次又一次地遇到同样的问题(当然是我).如今,您经常听到微前端的概念.这个概念是关于将前端设计为可扩展和可扩展的.特别是在许多公司同时拥有 AngularReactVue 开发人员的时代.我的研究得出了以下方法:

I've been very busy with the topic lately, because many have the same problem again and again (me, of course). Nowadays, you often hear the concept of micro-frontends. The concept is about designing the frontend to be extensible and scalable. Especially in the time when many companies have both Angular and React and Vue developers. The following approaches came out of my research:

链接:

您可以创建新应用并从旧应用中获得超链接.(在两个应用之间切换时,您必须重新加载页面.)

You can create your new app and have a hyperlink from your legacy app. (You must have a page reload when you switch beetween the both apps.)

内嵌框架:

软件架构师构建了一个元路由器来处理 iframe 和单页应用程序:
看看这个:

a Software architect builded a meta router to deal with iframes and single page application:
Take a look at this:

  • (github repository) A lightweight and solid approach towards micro frontends (SPAs/ clients for micro services)

元框架:

Metaframeworks 允许您在使用不同框架构建的 differents 应用之间进行通信:
这里有一些用于微前端目的的元框架示例.

Metaframeworks allow you to have a communication beetween differents apps builded using differents frameworks:
here some example of metaframeworks for microfrontend purposes.

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆