how to embed an angular app into another app?

前端 未结 3 1547
走了就别回头了
走了就别回头了 2020-12-02 11:49

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.

相关标签:
3条回答
  • 2020-12-02 12:09

    UPDATE 2020

    You can also use the new Webpack 5 Module federation.

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

    • Implementation examples of module federation , by the creators of module federation
    • Example for building a plugin-based workflow designer with Angular and Dynamic Module Federation
    • Dynamic Module Federation with Angular

    Original answer

    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:

    Links :

    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.)

    Iframes :

    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:

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

    • Single Spa (official website) Single Spa: a javascript framework for front-end microservices
    • FrintJs (official website) FrintJS: Modular JavaScript framework for building Scalable & Reactive applications.

    Web Components i.e Angular elements:

    In order to deal with npm packages you can use as the other answerer mentioned the concept of Angular elements. You have to create a shell app and another independently apps that will be registred as elements in your shell app. Take a look at this example.: Building micro frontends — angular elements

    Mosaiq: Layout service

    The online shop Zalando faced to the same problem and created a framework in order to deal with the problem:

    • The Mosaic9 https://www.mosaic9.org/

    Espacially the part Tailor.js, an open source system for the assembling the components on-demand on a backend layer written in Go.

    PS. Tailor was inspired by BigPipe: Pipelining web pages for high performance from Facebook.

    Plugin Architecture

    A plugin architecture is an architecture that will call external code at certain points without knowing all the details of that code in advance.

    This Stackoverflow question explain more about it in case of single page application:

    Angular libraries

    I Think a right way is to have in a app the same framework and the same version of this framework (e.g Angular 7). I will prefer to take a time a do a upgrade to a typescript version of angular. I hope the answer can be usefull for others.

    Other Stackoverflow related Q & A:

    Vue.JS - micro frontend approach
    Micro frontend architecture advice

    Micro frontends examples

    • Angular 8 and ReactJS (using NodeJS server and MySQL database): https://github.com/billyjov/angular-react-microfrontend

    List of ressources about micro frontends

    https://github.com/billyjov/microfrontend-resources

    0 讨论(0)
  • 2020-12-02 12:10

    I think the solution of your problem resides in Angular Elements. It is pretty cool and provided by angular team.

    Angular Elements lets you package your Angular components as custom web elements, which are part of the web components set of web platform APIs. Web components are technologies to help create reusable, encapsulated elements. Right now, that includes the shadow DOM, HTML templates, HTML imports, and custom elements. The custom elements technology powers Angular Elements.

    Here are some reference links from which you can learn more about Angular Elements.

    https://angular.io/guide/elements

    https://www.telerik.com/blogs/getting-started-with-angular-elements

    Thanks!

    0 讨论(0)
  • 2020-12-02 12:18

    I believe the Angular 6 Elements is going to be your only option for the non-angular apps. That is the only method that I am aware of that allows components to be executed outside of angular.

    In order to maintain your style encapsulation, you can use the css prefix:

    :host
    

    in your component's .css or .scss files.

    If you can get rid of the non-angular requirement, I would recommend the Angular CDK PortalHost and DomPortalHost.

    0 讨论(0)
提交回复
热议问题