使用 Angular 和 Module Federation 在仪表板中并排加载两个不同的应用程序
我有 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
解决方案是在捕获中提供一个占位符模块,并进一步在占位符[错误模块]中实现错误componet作为
The solution is to provide a placeholder module in catch and further implement errorcomponet in placeholder [error module] as