【问题标题】:How to implement micro frontend architecture in legacy application using Angular如何使用 Angular 在遗留应用程序中实现微前端架构
【发布时间】:2020-02-07 07:50:38
【问题描述】:

最近,我们的团队决定在我们的旧产品中实施微前端架构。它是使用 Asp.Net aspx 页面和 javascript/jquery 开发的。

去年我们开始在我们的应用程序中使用 Angular 来获取一些视图。为了加载 Angular,我们将 prod 构建文件放置在 .net 项目中,并将组件加载到 aspx 母版页中。

我们计划使用微前端架构将我们剩余的待处理旧视图迁移到 Angular。

所以我为此做了一个小型 poc,并且能够在接近它的某个地方实现架构。

我按照这个 url 实现并在端口 4400 上运行它。

https://medium.com/swlh/build-micro-frontends-using-angular-elements-the-beginners-guide-75ffeae61b58

在我现有的 Angular 项目中,我正在使用 customElements 加载它

this.appendCustomElementWithUrls('app-positions','http://localhost:4400/main-es5.js', (<HTMLElement>document.getElementById("chartAppContainerNamInqA")) );

appendCustomElementWithUrls(name: string,url: string,target: HTMLElement){
        if (!customElements.get(name)) {
              const script = document.createElement('script');
              script.src = url;
              document.head.appendChild(script);
          }

          const component = document.createElement(name);
          target.appendChild(component);

    }

这按预期工作,我可以在我的开发环境中加载 customElements。但是对于生产我真的不确定如何实施。

我的担心:

  1. 我是否也必须在 prod 的某个端口上运行应用程序?如果是,如何做到这一点,它是否可以是动态的,以便用户能够更改端口。我们在 .net 应用程序中的方式。由于客户端可能已经在该端口上运行了一些东西

  2. 我试图达到的方式是否正确。

提前致谢。

【问题讨论】:

    标签: c# angular asp.net-mvc .net-core micro-frontend


    【解决方案1】:

    对于那些可能有这样要求的人。

    我做了很多研究,浏览了很多文章,并得出了一个解决方案。

    所以我使用 Angular 元素创建了一个单独的应用程序,并使用 cmd 生成了单个包;

    ng build --prod --output-hashing none --single-bundle true
    

    然后我在 IIS 中创建了一个应用程序,并将所有 prod 生成的文件放在端口 9091 上。您可以使用任何端口。

    在我的 web.config 文件中,我创建了一个密钥,这样如果用户更改端口号,他们就会直接更新 web.config:

    <add key="MicroFrontEnd" value="http://localhost:9190"/>
    

    由于端口应该是可配置的,所以我创建了一个 api 来获取端口号。

    然后我在我的 shell 应用程序中使用了它,它就像一个魅力。

    【讨论】:

    • 您是否有示例如何将 Angular 集成到 aspx 页面中。
    • 嗨,约翰。在我的一个作为 root 的 aspx 页面中,我添加了一个 div
      我的 shell 应用程序查找此元素并加载应用程序
    猜你喜欢
    • 2022-01-23
    • 1970-01-01
    • 2023-02-11
    • 2023-01-31
    • 1970-01-01
    • 1970-01-01
    • 2011-08-02
    • 2021-09-07
    • 2010-12-21
    相关资源
    最近更新 更多