且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

角 6 |服务器端渲染:问题“窗口未定义"

更新时间:2023-02-23 17:39:26

假设您已经完成了 angular Universal 的 s-s-r.导入后将以下代码放入 server.ts 中.构建并运行.

const domino = require("domino");const fs = require("fs");const path = require("path");常量模板A = fs.readFileSync(path.join("dist/browser", "index.html")).toString();const win = domino.createWindow(templateA);win.Object = 对象;win.Math = Math;全局[窗口"] = 赢;global["document"] = win.document;全局[分支"] = null;global["object"] = win.object;global['HTMLElement'] = win.HTMLElement;global['navigator'] = win.navigator;global['localStorage'] = localStorage;

I have updated my application from angular version 5 to latest. Here's detail:

  Angular CLI: 6.2.1
  Node: 8.12.0
  OS: win32 x64
  Angular: 6.1.6

Also I am using this: ng-toolkit-universal to achieve server side rendering. I am able to build application using command "npm run build:prod" , but facing following issue while running command: "npm run server":

Can anyone please help or suggest some soution for the same.

Thanks in advance!

Assuming you are done s-s-r with angular universal. Put following code in your server.ts after imports. Build and run.

const domino = require("domino");
const fs = require("fs");
const path = require("path");
const templateA = fs
  .readFileSync(path.join("dist/browser", "index.html"))
  .toString();
const win = domino.createWindow(templateA);
win.Object = Object;
win.Math = Math;

global["window"] = win;
global["document"] = win.document;
global["branch"] = null;
global["object"] = win.object;
global['HTMLElement'] = win.HTMLElement;
global['navigator'] = win.navigator;
global['localStorage'] = localStorage;