微前端 - Module Federation使用完整示例

Angular 框架中

项目结构

复制代码
main-app/
  src/
    app/
      app.module.ts
      app.component.ts
micro-app/
  src/
    app/
      app.module.ts
      app.component.ts

主应用配置

安装必要依赖:

bash 复制代码
ng add @angular-architects/module-federation

修改 webpack.config.js

javascript 复制代码
const { share, SharedMappings } = require('@angular-architects/module-federation/webpack');

module.exports = {
  output: {
    uniqueName: "mainApp",
    publicPath: "auto"
  },
  optimization: {
    runtimeChunk: false
  },
  plugins: [
    new ModuleFederationPlugin({
      remotes: {
        "micro-app": "micro-app@http://localhost:4201/remoteEntry.js"
      },
      shared: share({
        "@angular/core": { singleton: true, strictVersion: true },
        "@angular/common": { singleton: true, strictVersion: true },
        "@angular/router": { singleton: true, strictVersion: true }
      })
    })
  ]
};

主应用路由配置 (app.module.ts):

typescript 复制代码
const routes: Routes = [
  {
    path: 'micro-app',
    loadChildren: () => import('micro-app/MicroModule').then(m => m.MicroModule)
  }
];

子应用配置

子应用 webpack.config.js

javascript 复制代码
const { share, SharedMappings } = require('@angular-architects/module-federation/webpack');

module.exports = {
  output: {
    uniqueName: "microApp",
    publicPath: "auto"
  },
  optimization: {
    runtimeChunk: false
  },
  plugins: [
    new ModuleFederationPlugin({
      name: "micro-app",
      filename: "remoteEntry.js",
      exposes: {
        './MicroModule': './src/app/micro.module.ts'
      },
      shared: share({
        "@angular/core": { singleton: true, strictVersion: true },
        "@angular/common": { singleton: true, strictVersion: true },
        "@angular/router": { singleton: true, strictVersion: true }
      })
    })
  ]
};

子应用模块 (micro.module.ts):

typescript 复制代码
@NgModule({
  declarations: [MicroComponent],
  imports: [
    CommonModule,
    RouterModule.forChild([
      { path: '', component: MicroComponent }
    ])
  ]
})
export class MicroModule { }

运行应用

  • 启动子应用:
bash 复制代码
ng serve micro-app --port 4201
  • 启动主应用:
bash 复制代码
ng serve main-app --port 4200

访问主应用路由 /micro-app 即可加载子应用模块。

共享服务示例:

假如想要创建host和remote项目共享的部分,我们可以使用共享服务。

创建共享服务 (shared-lib.service.ts):

typescript 复制代码
@Injectable({ providedIn: 'root' })
export class SharedLibService {
  public data$ = new BehaviorSubject<string>('Initial Value');
}

双方应用 的 webpack.config.js 中共享:

javascript 复制代码
shared: share({
  "shared-lib": { singleton: true, strictVersion: true }
})

动态加载组件

有个时候我们在host项目中,想要通过非常规的模式使用remote中的页面(常规方式是指官方指导文件中提到的方式,即路由方式),我们还可以怎么办呢?

在主应用中动态加载子应用组件:

typescript 复制代码
@Component({...})
export class HostComponent implements OnInit {
  constructor(private viewContainerRef: ViewContainerRef) {}

  async ngOnInit() {
    const m = await import('micro-app/Component');
    this.viewContainerRef.createComponent(m.MyExposedComponent);
  }
}

记得确保子应用暴露组件:

javascript 复制代码
exposes: {
  './Component': './src/app/my-component.ts'
}

详细的各种方式可以参考下面这位大大的github:

仓库地址https://github.com/edumserrano/webpack-module-federation-with-angulargithub.com

React 框架中:

因为本人目前只比较熟悉Angular框架,所以关于React,各位可以参考下面这位大大的github:

GitHub - module-federation/module-federation-examples: Implementation examples of module federation , by the creators of module federation

相关推荐
刘发财1 天前
弃用html2pdf.js,这个html转pdf方案能力是它的几十倍
前端·javascript·github
牛奶1 天前
2026年大模型怎么选?前端人实用对比
前端·人工智能·ai编程
牛奶1 天前
前端人为什么要学AI?
前端·人工智能·ai编程
Kagol1 天前
🎉OpenTiny NEXT-SDK 重磅发布:四步把你的前端应用变成智能应用!
前端·开源·agent
GIS之路1 天前
ArcGIS Pro 中的 notebook 初识
前端
JavaGuide1 天前
7 道 RAG 基础概念知识点/面试题总结
前端·后端
ssshooter1 天前
看完就懂 useSyncExternalStore
前端·javascript·react.js
格砸1 天前
从入门到辞职|从ChatGPT到OpenClaw,跟上智能时代的进化
前端·人工智能·后端
Live000001 天前
在鸿蒙中使用 Repeat 渲染嵌套列表,修改内层列表的一个元素,页面不会更新
前端·javascript·react native
柳杉1 天前
使用Ai从零开发智慧水利态势感知大屏(开源)
前端·javascript·数据可视化