javascript - 共享组件未在主机应用程序的 SSR 中呈现
问题描述
将 SSR 与模块联合使用时出现问题。 我的代码示例
如何开始
- 纱线安装:全部
- 纱线外壳:服务器:构建
- 纱线外壳:服务器:开始
- 纱线远程:开始
我有两个不同的应用程序,其中一个是带有 SSR 和渲染组件逻辑的主机应用程序,第二个是带有组件的共享库。我在主机应用程序中配置了 ng-universal,并希望在加载页面时看到完整的代码响应,但我只从主机应用程序获得代码。 我的回复截图 该代码让我有机会从 remoteEntry.js 获取组件
import { LoadRemoteModuleOptions } from '../interfaces/module-federation.interfaces';
type Scope = unknown;
type Factory = () => any;
type Container = {
init(shareScope: Scope, initScope?: Scope): void;
get(module: string, getScope?: Scope): Promise<Factory>;
};
const createContainer = (name: string): Container => {
// @ts-ignore
const container = window[name] as Container;
return container
} ;
declare const __webpack_init_sharing__: (shareScope: string) => Promise<void>;
declare const __webpack_share_scopes__: { default: Scope, plugin: Scope };
const moduleMap: any = {};
export function loadRemoteEntry(remoteEntry: string): Promise<boolean> {
return new Promise<any>((resolve, reject) => {
if (moduleMap[remoteEntry]) {
resolve(moduleMap[remoteEntry]);
return;
}
const script = document.createElement('script');
script.src = remoteEntry;
script.onerror = reject;
script.onload = () => {
moduleMap[remoteEntry] = true;
resolve(moduleMap[remoteEntry]); // window is the global namespace
};
document.body.appendChild(script);
});
}
async function lookupExposedRemote<T>(
remoteName: string,
exposedModule: string
): Promise<T> {
// Initializes the share scope. This fills it with known provided modules from this build and all remotes
await __webpack_init_sharing__('default');
// @ts-ignore
// @ts-ignore
const container = createContainer(remoteName);
if (!container) {throw new Error('Container with script is not defined')}
await container.init(__webpack_share_scopes__.default);
const factory = await container.get(exposedModule);
const Module = factory();
return Module as T;
}
export async function loadRemoteModule(
options: LoadRemoteModuleOptions
): Promise<any> {
await loadRemoteEntry(options.remoteEntry);
return await lookupExposedRemote<any>(
options.remoteName,
options.exposedModule
);
}
我认为问题是因为我在服务器端没有对象 Window,但我为此使用了多米诺骨牌。也许有人可以帮助我,我将不胜感激。
解决方案
问题解决了。只需构建远程应用程序并使用 lite 服务器启动它。回购已更新。如果您需要,请使用它。 https://i.imgur.com/pkyTEsQ.png
推荐阅读
- mulesoft - Mule 4/DW 2.0 - 在地图内循环出现问题
- java - 如何在 Netbeans 中使用模块路径进行构建
- spring - 是否可以根据与数据库的连接不可用来禁用 Spring Boot 数据源配置
- r - 使用for循环过滤R中的数据框
- google-chrome-devtools - 如何让 Chrome Devtools 按字母顺序对范围属性进行排序?
- cmake - 如何使用 ExternalProject_Add 进行简单的 make 调用?
- java - 无法在 FileMapping 中打开库
- c++ - 为什么我的程序在读取/写入文件时会丢弃最重要的数字?
- sql - 需要图书馆数据库中最近 30 天的数据
- deep-learning - 特定对之间的多 GPU 对等缓慢