首页 > 解决方案 > ReferenceError:未定义窗口 Angular Universal

问题描述

我正在使用 Angular 10 并尝试在我的项目中实现 SSR。

当我运行时,npm run serve:ssr我收到以下错误

ReferenceError: window is not defined

当我用谷歌搜索时,他们建议添加domino

所以下面是我的 server.ts

....
const scripts = fs.readFileSync('dist/asfc-web/browser/index.html').toString();

const window = domino.createWindow(scripts);
global['window'] = window;
global['document'] = window.document;

....

仍然出现相同的错误,请指导我如何解决此问题。

标签: node.jsangulartypescriptserver-side-renderingangular-universal

解决方案


很简单的解决方法

我已经导入了AppServerModuleafterglobal['window']并且它有效

global['window'] = window;
global['document'] = window.document;

import { AppServerModule } from '../../projects/asfc-web/src/main.server';

推荐阅读