From 011a0d16abe9946d7136509241341e7d76e69982 Mon Sep 17 00:00:00 2001 From: jxxghp Date: Sat, 10 May 2025 08:40:14 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8A=A0=E8=BD=BD=E8=BF=9C=E7=A8=8B=E7=BB=84?= =?UTF-8?q?=E4=BB=B6=E6=97=B6=E5=A6=82=E6=9C=AA=E6=B3=A8=E5=86=8C=E5=88=99?= =?UTF-8?q?=E9=87=8D=E6=96=B0=E6=B3=A8=E5=86=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/utils/federationLoader.ts | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/src/utils/federationLoader.ts b/src/utils/federationLoader.ts index 1b368373..461d9766 100644 --- a/src/utils/federationLoader.ts +++ b/src/utils/federationLoader.ts @@ -12,6 +12,20 @@ interface RemoteModule { url: string } +/** + * 获取单个远程模块信息 + * @param id 远程模块ID + */ +async function fetchSingleRemoteModule(id: string): Promise { + try { + const modules = await fetchRemoteModules() + return modules.find(module => module.id === id) || null + } catch (error) { + console.error(`获取远程模块信息失败: ${id}`, error) + return null + } +} + /** * 加载远程组件 * @param id 远程模块ID @@ -22,8 +36,24 @@ export async function loadRemoteComponent(id: string, componentName: string = 'P const module = await __federation_method_getRemote(id, `./${componentName}`) return __federation_method_unwrapDefault(module) } catch (error) { - console.error(`加载远程组件失败: ${id}/${componentName}`, error) - throw error + // 组件未注册,尝试重新注册 + try { + const moduleInfo = await fetchSingleRemoteModule(id) + if (moduleInfo) { + console.log(`组件未注册,正在重新注册: ${id}`) + injectRemoteModule(moduleInfo) + + // 重新尝试加载组件 + const module = await __federation_method_getRemote(id, `./${componentName}`) + return __federation_method_unwrapDefault(module) + } else { + console.error(`无法找到远程模块信息: ${id}`) + throw new Error(`无法找到远程模块信息: ${id}`) + } + } catch (retryError) { + console.error(`重新注册并加载组件失败: ${id}/${componentName}`, retryError) + throw retryError + } } }