在 React shell 应用程序中使用 Angular Elements MFE Web 组件

发布于 2025-01-12 21:26:18 字数 1590 浏览 5 评论 0原文

我正在尝试使用 React shell 和 Angular 微前端应用程序构建一个微前端项目。

我不知道如何将 remoteEntry.js 导入 React 组件并使用 Angular Elements 生成的 Web 组件。

// const Catalog = React.lazy(() => import('HorizontalCatalogMfe/web-components'));

export function App() {
  return (
    <>
      <h1>horizontal-shell-react</h1>
      <div />

      {/* not working :( */}
      <horizontal-catalog-mfe></horizontal-catalog-mfe>
    </>
  );
}

export default App;

我的 shell 模块联合配置:

plugins: [
  new ModuleFederationPlugin({
    name: 'HorizontalShellReact',
    remotes: {
      HorizontalCatalogMfe: 'HorizontalCatalogMfe@http://localhost:4201/remoteEntry.js',
    },
  })
]

目录 MFE Angular - 模块联合配置:

plugins: [
  new ModuleFederationPlugin({
    name: 'HorizontalCatalogMfe',
    exposes: {
      './web-components': 'apps/horizontal-catalog-mfe/src/bootstrap.ts'
    },
    filename: 'remoteEntry.js',
  })
]

Angular Elements 配置:

@NgModule({
  declarations: [AppComponent],
  imports: [
    BrowserModule,
    RouterModule.forRoot([], { initialNavigation: 'enabledBlocking' }),
    RemoteEntryModule,
  ],
  providers: [],
  //bootstrap: [AppComponent],
})
export class AppModule implements DoBootstrap {
  constructor(private injector: Injector) {}

  ngDoBootstrap() {
    const ce = createCustomElement(RemoteEntryComponent, {
      injector: this.injector,
    });
    customElements.define('horizontal-catalog-mfe', ce);
  }
}

请告知。

I'm trying to build a micro-frontend project with React shell and Angular micro-frontend application.

I don't have any idea how to import remoteEntry.js into React component and use the web component generated by Angular Elements.

// const Catalog = React.lazy(() => import('HorizontalCatalogMfe/web-components'));

export function App() {
  return (
    <>
      <h1>horizontal-shell-react</h1>
      <div />

      {/* not working :( */}
      <horizontal-catalog-mfe></horizontal-catalog-mfe>
    </>
  );
}

export default App;

My shell Module Federation config:

plugins: [
  new ModuleFederationPlugin({
    name: 'HorizontalShellReact',
    remotes: {
      HorizontalCatalogMfe: 'HorizontalCatalogMfe@http://localhost:4201/remoteEntry.js',
    },
  })
]

Catalog MFE Angular - Module Federation config:

plugins: [
  new ModuleFederationPlugin({
    name: 'HorizontalCatalogMfe',
    exposes: {
      './web-components': 'apps/horizontal-catalog-mfe/src/bootstrap.ts'
    },
    filename: 'remoteEntry.js',
  })
]

Angular Elements Config:

@NgModule({
  declarations: [AppComponent],
  imports: [
    BrowserModule,
    RouterModule.forRoot([], { initialNavigation: 'enabledBlocking' }),
    RemoteEntryModule,
  ],
  providers: [],
  //bootstrap: [AppComponent],
})
export class AppModule implements DoBootstrap {
  constructor(private injector: Injector) {}

  ngDoBootstrap() {
    const ce = createCustomElement(RemoteEntryComponent, {
      injector: this.injector,
    });
    customElements.define('horizontal-catalog-mfe', ce);
  }
}

Please advise.

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文