问题
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