ReferenceError: window is not defined Angular Universal

自古美人都是妖i 提交于 2021-02-10 14:14:38

问题


I'm using Angular 10 and trying to implement SSR in my project.

When I run the npm run serve:ssr I'm getting the below error

ReferenceError: window is not defined

When I googled, they suggested to add domino

So below is my 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;

....

still getting the same error, Please guide me how to resolve this issue.


回答1:


It's simple fix,

I've imported the AppServerModule after the global['window'] and it worked

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

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


来源:https://stackoverflow.com/questions/63048298/referenceerror-window-is-not-defined-angular-universal

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!