简体   繁体   English

在主机 Angular 应用程序中使用 Angular 微前端遥控器的不同方法

[英]Different approach to utilize Angular Micro-Frontend remotes in the host Angular app

Context:语境:

I have 2 Angular 13 applications, 1 as remote and the other as host and using Module Federation to configure both remote(creating mfe) and host.我有 2 Angular 13 个应用程序,1 个作为远程应用程序,另一个作为主机,并使用 Module Federation 配置远程(创建 mfe)和主机。

All Module Federation examples directs us to load the remote module in the host like this所有 Module Federation 示例都指导我们像这样在主机中加载远程模块

 {
    path: 'kpi',
    loadChildren: () =>
      loadRemoteModule({
        remoteEntry:
          'http://localhost:58777/dist/megaHrx/remoteEntry.js',
        remoteName: 'mfe1',
        exposedModule: './KpiModule'
      }).then((m) => m.ChangeKpiModule)
  }

While my remote Module is as following:而我的远程模块如下:

@NgModule({
  declarations: [ChangeKpiComponent],
  imports: [CommonModule, ChangeKpiRoutingModule],
  providers: [AbbbbbService , StorageService]
})
export class ChangeKpiModule {}

Question:问题:

This works and my Remote component ChangeKpiComponent gets rendered under the route kpi as expected, but what my intention is to use this ChangeKpiComponent via its selector admin-reporting-change-kpi and not by directly loading it via loadChildren in the Routing module.这有效,我的远程组件ChangeKpiComponent按预期在路由kpi下呈现,但我的意图是通过其选择器admin-reporting-change-kpi使用此ChangeKpiComponent ,而不是通过 Routing 模块中的 loadChildren 直接加载它。

Is there a way to do that?有没有办法做到这一点? If yes, How?如果是,如何?

I think you can achieve that as follows:我认为您可以通过以下方式实现:

  • Expose the component itself also from the remote app (not only the module).也从远程应用程序(不仅是模块)公开组件本身。
  • Load it in your host app using the loadRemoteModule method.使用loadRemoteModule方法将其加载到您的主机应用程序中。
  • Create a dynamic component based on the returned type from loadRemoteModule .根据从loadRemoteModule返回的类型创建一个动态组件
  • You can create a directive to reuse it anywhere in the host app:您可以创建一个指令以在主机应用程序的任何地方重用它:
@Directive({ selector: '[adHost]' })
export class AdHostDirective {
  constructor(private viewContainerRef: ViewContainerRef) {}

  ngOnInit(): void {
    loadRemoteModule({
      remoteEntry: 'http://localhost:58777/dist/megaHrx/remoteEntry.js',
      remoteName: 'mfe1',
      exposedModule: './KpiComponent', // <<<< Use the component expose
    }).then((m) => {
      const componentType = m.ChangeKpiComponent; // <<<< The component's type here.

      // Create the component in this view-container:
      this.viewContainerRef.createComponent(componentType);
    });
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用微前端运行时集成方法在同一页面上显示反应和 angular? - How to show react and angular on same page using micro-frontend Runtime integration approach? Angular 14 微前端项目,无法打开模态窗口/对话框 window - Angular 14 Micro-frontend project, unable to open modal window/dialog window Angular 微前端方法 - Angular micro frontends approach 在同一页面(根)上渲染 2 个微前端 - Render 2 micro-frontend on the same page (root) 是否可以在 Angular 容器微前端中将 Angular JS 作为微前端运行? - Is it possible to run Angular JS as a micro frontend in an Angular container micro frontend? 从主应用程序(微前端)将参数发送到 Angular 元素 - send param to Angular Element from Master App (Micro FrontEnd) Angular 微前端微应用的服务 url 不断被 Shell 应用的 Z572D12E421E5E06B7B 取代 - Angular Micro Frontend micro-app's services url keeps getting replaced by Shell app's url 微前端 Angular 11 销毁问题 - Micro Frontend Angular 11 Destroy Issue 如何在 Angular 中实现微前端(Iframe 的替代方案) - How to Implement Micro Frontend in Angular (Alternative to Iframe) 如何在微前端中的 2 个应用程序与 webpack 模块联合(角度)之间建立关系 - how can I relation between 2 app in micro frontend with webpack module federation (angular)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM