Angular Module Federation 加载远程应用时出现无限刷新问题求助
Angular Module Federation 加载远程应用时出现无限刷新问题求助
大家好,我正在尝试把我的Angular 20单体应用拆分成微前端结构,把博客模块单独抽成一个远程应用。所有基础配置都搭好了,但现在遇到一个棘手的问题:只要远程应用的入口文件被加载,我的主应用就开始无限刷新,根本停不下来😭
下面是我的相关配置和代码,麻烦帮忙看看哪里出问题了:
Webpack配置
主应用(Shell)的Webpack配置
const { withModuleFederationPlugin, shareAll } = require('@angular-architects/module-federation/webpack'); const config = withModuleFederationPlugin({ name: 'host', exposes: {}, remotes: { blog: 'blog@localhost:4201/remoteEntry.js', }, shared: { } }); config.output.uniqueName = 'shell'; config.optimization.runtimeChunk = false; module.exports = config;
远程博客应用的Webpack配置
const { withModuleFederationPlugin } = require('@angular-architects/module-federation/webpack'); const config = withModuleFederationPlugin({ name: 'blog', filename: 'remoteEntry.js', exposes: { './BlogLandingPageComponent': './projects/blog/src/app/blog/blog-landing-page/blog-landing-page.component', './BlogService': './projects/blog/src/app/blog/blog.service', }, shared: { }, remoteType: 'var', library: { type: 'var', name: 'blog' }, }); config.output = { chunkFormat: 'array-push', chunkLoading: 'jsonp', uniqueName: 'blog', publicPath: "auto", // 用自动公共路径做动态加载 scriptType: 'text/javascript' }; config.optimization = { runtimeChunk: false, }; module.exports = config;
主应用main.ts中加载远程入口的代码
Promise.all([ loadRemoteEntry({ type: 'script', remoteEntry: "http://localhost:4201/remoteEntry.js", remoteName: 'blog' }), ]) .catch((err) => console.error('Error loading remote entries', err)) .then((val) => { console.log('Remote entry loaded successfully:', val); // 检查远程容器是否可用 if (window['blog']) { console.log('✅ 远程容器"blog"已可用:', window['blog']); } else { console.error('❌ 远程容器"blog"不可用!'); } if (document.readyState === 'complete') { bootstrap(); } else { document.addEventListener('DOMContentLoaded', bootstrap); } })
我发现一个关键现象:只要把loadRemoteEntry这段代码注释掉,无限刷新的问题就完全消失了。我现在怀疑是不是热模块重载(HMR)导致的冲突?有没有朋友遇到过类似的问题,或者能给我一些排查的方向?
内容来源于stack exchange
相关产品推荐
相关产品推荐

