微前端 - 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

相关推荐
Jmyd01235 小时前
Web端3D营销展厅技术实现路径:从素材上云到线索回流的数据链路
前端·3d·性能优化·三维数字化
砚凝霜5 小时前
【软考信息安全】第二十二章 Web服务器安全分析与增强——Apache与IIS
服务器·前端·安全
u0111026757 小时前
JPG、PNG、WebP 怎么选?按透明度、体积和使用场景做判断
前端·javascript·vue.js·图像处理·ai作画
jerrywus8 小时前
让 AI 写完代码后交一份报告:我改造了 answer-me-with-html
前端·claude·cursor
派小心.8 小时前
小程序分享链路中来源参数的传递校验
大数据·前端·数据库·人工智能·小程序·数据分析
hhb_6189 小时前
一键生成数据看板,ECharts实战演示
前端·javascript·echarts
Java后端的Ai之路9 小时前
08_React组件API
前端·react.js·前端框架
小马不起床9 小时前
零基础玩转 FastAPI_09:前端基础工程构建
前端·状态模式·fastapi
风骏时光牛马10 小时前
用户管理模块接口开发
前端
彭亚川Allen10 小时前
从零手搓 RAG 之检索全流程,给大模型接上自己的知识库
前端·后端·ai编程