使用 Angular 和 Module Federation 在仪表板中并排加载两个不同的应用程序

发布于 2025-01-18 09:41:56 字数 2743 浏览 3 评论 0原文

我有 2 个不同的 Angular 应用程序 [产品视图和产品购物车] 和一个 shell 应用程序。所有 3 个应用程序都托管在不同的端口上。在 shell 应用程序中,我使用模块联合集成了上述应用程序。这两个应用程序完美适用于不同的路线。这是我的 shell 应用程序路由代码:

 const routes: Routes = [
  {
    path: '',
    component: HomeComponent,
    children : [
      {
        path: '',
        outlet:'pView',
        loadChildren: () =>
          loadRemoteModule({
            remoteEntry: 'http://localhost:3000/remoteEntry.js',
            remoteName: 'mfe1',
            exposedModule: './Module',
          }).then((m) => {
            return m.MicrofrontendModule;
          }),
      },
      {
        path: '',
        outlet:'pCart',
        loadChildren: () =>
          loadRemoteModule({
            remoteEntry: 'http://localhost:7000/remoteEntry.js',
            remoteName: 'mfe2',
            exposedModule: './Module1',
          }).then((m) => {
            return m.Microfrontend1Module;
          }).catch(err => console.log('Error loading remote entries', err)),
      },    
    ],
    pathMatch: 'full',
  },
  {
    path: 'child',
    outlet:'',
    loadChildren: () =>
      loadRemoteModule({
        remoteEntry: 'http://localhost:8000/remoteEntry.js',
        remoteName: 'mfe3',
        exposedModule: './Module3',
      }).then((m) => {
        return m.Microfrontend2Module;
      }),
  },
];

我的用于加载上述 2 个应用程序的 html 代码如下:

<div class="container" style="width: 99%;">
<table class="table" style="width:100%;margin-top: 0px; border: 2px solid white">
  <tbody>
    <tr>
      <td>
        <div style="width: 500px; height: 370px; border: 1px solid rosybrown">
          <router-outlet name="pView"></router-outlet>
        </div>
      </td>
      <td>
        <div style="width: 500px; height: 370px; border: 1px solid green">
          <router-outlet name="pCart"></router-outlet>
        </div>
      </td>
    </tr>

它工作正常,但在我的一个远程应用程序关闭后出现以下错误。

GET http://localhost:7000/remoteEntry.js net::ERR_CONNECTION_REFUSED

我尝试在 main.ts 中的 Shell 应用程序 Bootstrap 之前加载远程应用程序,

import { loadRemoteEntry } from '@angular-architects/module-federation';

Promise.all([
   loadRemoteEntry('http://localhost:3000/remoteEntry.js','mfe1'),
   loadRemoteEntry('http://localhost:7000/remoteEntry.js','mfe2'),
   loadRemoteEntry('http://localhost:8000/remoteEntry.js','mfe3')       
])
.catch(err => console.log('Error loading remote entries', err))
.then(() => import('./bootstrap'))
.catch(err => console.log(err));


// import('./bootstrap').catch(err => console.error(err));

但仍然遇到相同的错误。任何人都可以帮助我解决这个问题。

I have 2 different Angular apps [Product View and Product Cart] and a shell application. All 3 apps are hosted on different ports. In the shell application I have integrated above apps using module federation. Both apps perfectly work for different routes. Here is my shell app routing code:

 const routes: Routes = [
  {
    path: '',
    component: HomeComponent,
    children : [
      {
        path: '',
        outlet:'pView',
        loadChildren: () =>
          loadRemoteModule({
            remoteEntry: 'http://localhost:3000/remoteEntry.js',
            remoteName: 'mfe1',
            exposedModule: './Module',
          }).then((m) => {
            return m.MicrofrontendModule;
          }),
      },
      {
        path: '',
        outlet:'pCart',
        loadChildren: () =>
          loadRemoteModule({
            remoteEntry: 'http://localhost:7000/remoteEntry.js',
            remoteName: 'mfe2',
            exposedModule: './Module1',
          }).then((m) => {
            return m.Microfrontend1Module;
          }).catch(err => console.log('Error loading remote entries', err)),
      },    
    ],
    pathMatch: 'full',
  },
  {
    path: 'child',
    outlet:'',
    loadChildren: () =>
      loadRemoteModule({
        remoteEntry: 'http://localhost:8000/remoteEntry.js',
        remoteName: 'mfe3',
        exposedModule: './Module3',
      }).then((m) => {
        return m.Microfrontend2Module;
      }),
  },
];

And my html code to load above 2 apps is as below :

<div class="container" style="width: 99%;">
<table class="table" style="width:100%;margin-top: 0px; border: 2px solid white">
  <tbody>
    <tr>
      <td>
        <div style="width: 500px; height: 370px; border: 1px solid rosybrown">
          <router-outlet name="pView"></router-outlet>
        </div>
      </td>
      <td>
        <div style="width: 500px; height: 370px; border: 1px solid green">
          <router-outlet name="pCart"></router-outlet>
        </div>
      </td>
    </tr>

It is working fine but I am getting below error after one of my remote app is down.

GET http://localhost:7000/remoteEntry.js net::ERR_CONNECTION_REFUSED

I have tried to load remote apps before Shell app Bootstrap in main.ts as

import { loadRemoteEntry } from '@angular-architects/module-federation';

Promise.all([
   loadRemoteEntry('http://localhost:3000/remoteEntry.js','mfe1'),
   loadRemoteEntry('http://localhost:7000/remoteEntry.js','mfe2'),
   loadRemoteEntry('http://localhost:8000/remoteEntry.js','mfe3')       
])
.catch(err => console.log('Error loading remote entries', err))
.then(() => import('./bootstrap'))
.catch(err => console.log(err));


// import('./bootstrap').catch(err => console.error(err));

But still getting same error. Anyone kindly help me on this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

贪了杯 2025-01-25 09:41:56

解决方案是在捕获中提供一个占位符模块,并进一步在占位符[错误模块]中实现错误componet作为

loadChildren: () =>
      loadRemoteModule({
        remoteEntry: 'http://localhost:8000/remoteEntry.js',
        remoteName: 'mfe3',
        exposedModule: './Module3',
      }).then((m) => {
        return m.Microfrontend2Module;
      })
      .catch(e => {
        return import('src/app/placeholder/error.module').then(m => m.ErrorModule);
     })
  },

The solution is to provide a placeholder module in catch and further implement errorcomponet in placeholder [error module] as

loadChildren: () =>
      loadRemoteModule({
        remoteEntry: 'http://localhost:8000/remoteEntry.js',
        remoteName: 'mfe3',
        exposedModule: './Module3',
      }).then((m) => {
        return m.Microfrontend2Module;
      })
      .catch(e => {
        return import('src/app/placeholder/error.module').then(m => m.ErrorModule);
     })
  },
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文