当前位置: 首页 > 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

 

相关文章:

  • 深入理解PHP安全漏洞:文件包含与SSRF攻击全解析
  • 「Java基本语法」代码格式与注释规范
  • K8S认证|CKS题库+答案| 6. 创建 Secret
  • NLP学习路线图(三十):微调策略
  • 【K8S系列】Kubernetes 中 Pod(Java服务)启动缓慢的深度分析与解决方案
  • YOLO11解决方案之分析
  • PyTorch 中contiguous函数使用详解和代码演示
  • 【科研绘图系列】R语言绘制论文组图(multiple plots)
  • 【20250607接单】Spark + Scala + IntelliJ 项目的开发环境配置从零教学
  • Redis 实现分布式锁:深入剖析与最佳实践(含Java实现)
  • 【精选】计算机毕业设计Python Flask海口天气数据分析可视化系统 气象数据采集处理 天气趋势图表展示 数据可视化平台源码+论文+PPT+讲解
  • Java实现飞机射击游戏:从设计到完整源代码
  • ubuntu 22.04虚拟机配置静态IP
  • OpenWrt:使用ALSA实现边录边播
  • 【数据结构】6. 时间与空间复杂度
  • Docker镜像无法拉取问题解决办法
  • Linux内核 - 日志输出系统
  • 手机App-插入USB时自动授权点击确定按钮-使系统弹出框自动消失
  • h5 安卓手机去掉滚动条问题
  • Addressable-配置相关
  • 做美食网站需求分析报告/爱站网关键词密度
  • 全球网站排名查询/谈谈你对互联网营销的认识
  • 免费制作一个自己的网站吗/服务营销的七个要素
  • 用html5做的网站的原代码/青岛网站关键词优化公司
  • 网件路由器做网站/手机百度搜索app
  • 地方网站发展/百度识图网页入口