当前位置: 首页 > news >正文

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

Angular 框架中

项目结构

main-app/src/app/app.module.tsapp.component.ts
micro-app/src/app/app.module.tsapp.component.ts

主应用配置

安装必要依赖:

ng add @angular-architects/module-federation

修改 webpack.config.js

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):

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

子应用配置

子应用 webpack.config.js

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):

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

运行应用

  • 启动子应用:
ng serve micro-app --port 4201
  • 启动主应用:
ng serve main-app --port 4200

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

共享服务示例:

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

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

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

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

shared: share({"shared-lib": { singleton: true, strictVersion: true }
})

动态加载组件

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

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

@Component({...})
export class HostComponent implements OnInit {constructor(private viewContainerRef: ViewContainerRef) {}async ngOnInit() {const m = await import('micro-app/Component');this.viewContainerRef.createComponent(m.MyExposedComponent);}
}

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

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

 

http://www.xdnf.cn/news/915463.html

相关文章:

  • 《经济学原理》第9版第6章供给、需求和政府政策
  • XSS(跨站脚本攻击)详解
  • linux 用户态时间性能优化工具perf/strace/gdb/varlind/gprof
  • jvm 垃圾收集算法 详解
  • UDP 与 TCP 调用接口的差异:面试高频问题解析与实战总结
  • html如何在一张图片上的某一个区域做到点击事件
  • 【Docker 01】Docker 简介
  • git小乌龟不显示图标状态解决方案
  • 分组背包问题Python和C++两个版本讲解
  • Git 使用完全指南:从入门到协作开发
  • 鸿蒙仓颉语言开发实战教程:商城应用个人中心页面
  • MCP 技术完全指南:微软开源项目助力 AI 开发标准化学习
  • 【K8S系列】Kubernetes 中 Pod(Java服务)启动缓慢的深度分析与解决方案
  • api将token设置为环境变量
  • 503 Service Unavailable:服务器暂时无法处理请求,可能是超载或维护中如何处理?
  • HAL库开发--SPI的配置方式和读写操作
  • 《从零掌握MIPI CSI-2: 协议精解与FPGA摄像头开发实战》-- CSI-2 协议详细解析LLP (二)
  • pycharm中提示C++ compiler not found -- please install a compiler
  • K8S认证|CKS题库+答案| 5.日志审计
  • 容器安全最佳实践:云原生环境下的零信任架构实施
  • java复习 04
  • HTML面试整理
  • 深入了解UDP套接字:构建高效网络通信
  • iframe(概念、简单例子、在vue项目中的使用)
  • Java毕业设计:WML信息查询与后端信息发布系统开发
  • TripGenie:畅游济南旅行规划助手:个人工作纪实(二十二)
  • [总结篇]个人网站
  • 应用层协议:HTTPS
  • 微软PowerBI考试 PL300-使用适用于 Power BI 的 Copilot 创建交互式报表
  • 2025年- H75-Lc183--121.买卖股票的最佳时机1(贪心or动态规划)--Java版