Angular构建后dist多项目协同:App1如何配置路由加载App2、App3组件
实现逻辑说明
你当前是手动集成独立构建的Angular产物做本地测试,没有使用正式的微前端框架,核心实现思路是:路由匹配到对应路径时,动态加载App2、App3的独立打包脚本,待脚本加载完成后,调用远程应用暴露的启动方法,将应用渲染到App1指定的容器节点上,不依赖Angular原生的模块懒加载机制。
具体配置步骤
- 整理本地测试的资源路径结构
不要将App2、App3的产物直接堆在App1构建产物的根目录,避免和App1自身的静态资源路径冲突:- 在App1的dist目录下新建固定的远程应用存放路径:
remote-apps/app2、remote-apps/app3 - 单独构建App2、App3时,将
angular.json中的outputHashing配置为none,避免生成带随机hash的文件名导致路径不固定 - 构建完成后,把App2的
main.js拷贝到dist/app1/remote-apps/app2/main.js,把App3的main.js拷贝到dist/app1/remote-apps/app3/main.js
- 在App1的dist目录下新建固定的远程应用存放路径:
- 改造远程应用的启动入口
App2、App3单独构建时,不要在自身的main.ts中写死挂载到默认的app-root节点,要把启动方法暴露成全局可调用的方法,支持外部传入自定义的渲染容器,以App2为例:
App3的入口按照同样逻辑改造,暴露// app2/src/main.ts 改造后的代码 import { bootstrapApplication } from '@angular/platform-browser'; import { App2RootComponent } from './app/app2.component'; // 暴露全局启动方法,供App1调用 (window as any).bootstrapApp2 = (containerEl: Element) => { return bootstrapApplication(App2RootComponent, { providers: [] }); };bootstrapApp3全局方法即可。 - 在App1中实现通用的脚本加载工具
编写工具方法实现动态脚本加载,同时做防重复加载处理,避免路由多次跳转时重复拉取脚本:// app1/src/utils/load-remote-script.ts const loadedScriptCache = new Map<string, Promise<void>>(); export function loadRemoteScript(scriptUrl: string): Promise<void> { if (loadedScriptCache.has(scriptUrl)) { return loadedScriptCache.get(scriptUrl)!; } const loadTask = new Promise<void>((resolve, reject) => { const scriptElement = document.createElement('script'); scriptElement.src = scriptUrl; scriptElement.onload = () => resolve(); scriptElement.onerror = () => reject(new Error(`远程脚本加载失败:${scriptUrl}`)); document.body.appendChild(scriptElement); }); loadedScriptCache.set(scriptUrl, loadTask); return loadTask; } - 编写远程应用的统一路由入口组件
新建一个可复用的组件,专门用来承载远程应用的渲染:
组件模板(remote-entry.component.html)非常简单,只需要放一个空的渲染容器:
组件逻辑(<div #remoteContainer></div>remote-entry.component.ts)负责接收传入的脚本路径、启动方法名,在初始化时加载对应远程应用并完成渲染:import { Component, OnInit, Input, ViewChild, ElementRef } from '@angular/core'; import { loadRemoteScript } from '../../utils/load-remote-script'; @Component({ selector: 'app-remote-entry', templateUrl: './remote-entry.component.html' }) export class RemoteEntryComponent implements OnInit { @Input() scriptPath!: string; @Input() bootMethodName!: string; @ViewChild('remoteContainer') containerRef!: ElementRef; ngOnInit() { loadRemoteScript(this.scriptPath).then(() => { const bootFn = (window as any)[this.bootMethodName]; if (bootFn) { bootFn(this.containerRef.nativeElement); } }); } } - 在App1的路由表中配置对应规则
不使用原生的loadChildren(该方法仅支持加载App1本地编译的模块),而是将App2、App3的路径指向上面编写的统一入口组件,通过路由data传入对应的配置参数:// app1/src/app/app-routing.module.ts const routes: Routes = [ // App1自身的本地路由配置 { path: 'home', component: HomePageComponent }, // App2的路由匹配规则 { path: 'app2/**', component: RemoteEntryComponent, data: { scriptPath: '/remote-apps/app2/main.js', bootMethodName: 'bootstrapApp2' } }, // App3的路由匹配规则 { path: 'app3/**', component: RemoteEntryComponent, data: { scriptPath: '/remote-apps/app3/main.js', bootMethodName: 'bootstrapApp3' } }, { path: '**', redirectTo: 'home' } ];
本地测试的注意事项
- 每次修改App2、App3的代码后,需要重新执行独立构建,再将新生成的
main.js拷贝到App1对应的远程目录,避免加载到旧版本的脚本 - 要提前处理跨应用的样式冲突,可通过Angular的视图封装机制(如
ViewEncapsulation.ShadowDom)隔离不同应用的样式,避免互相污染 - 要保证路由路径的前缀和App2、App3内部的路由逻辑匹配,比如App2内部的页面路径是
/detail,对外访问的完整路径应该是/app2/detail,避免路由跳转时出现路径不匹配的问题 - 不要对远程应用的
main.js开启打包后的资源哈希配置,否则手动拷贝时文件名会动态变化,导致路由配置的固定路径加载失败
内容的提问来源于stack exchange,提问作者gbr
相关产品推荐
相关产品推荐

