未定义Angular 7 SSR窗口

时间:2019-04-03 19:51:42

标签: angular7 ssr

我对Angular 7 SSR服务器端渲染窗口有问题,并且在服务器站点上找不到文档。运行npm run build:ssr && npm run serve:ssr时,显示参考错误的终端看起来像ReferenceError: window is not defined

ngOnInit() {
    window.scrollTo(0, 0);
}

1 个答案:

答案 0 :(得分:0)

写入server.ts

/ ssr DOM
const domino = require('domino');
const fs = require('fs');
const path = require('path');
// index from browser build!
const template = fs.readFileSync(path.join(__dirname, '.', 'dist', 'index.html')).toString();
// for mock global window by domino
const win = domino.createWindow(template);
// from server build
const files = fs.readdirSync(`${process.cwd()}/dist-server`);
// mock
global['window'] = win;

更多{@ {3}}