繁体   English   中英

Angular Universal ReferenceError-未定义KeyboardEvent

[英]Angular Universal ReferenceError - KeyboardEvent is not defined

我在server.ts中添加了“ domino”,甚至将webpack.server.config.js更新为:

module: {
    rules: [
              { test: /\.(ts|js)$/, loader: 'regexp-replace-loader', options: { match: { pattern: '\\[(Mouse|Keyboard)Event\\]', flags: 'g' }, replaceWith: '[]', } },
              { test: /\.ts$/, loader: 'ts-loader' }, 
           ]
}

但是仍然出现相同的错误:“ ReferenceError-未定义KeyboardEvent”。

我在终端中运行这些命令

$ npm运行build:ssr

$ npm run serve:ssr

package.json中定义的命令为:

"build:ssr": "npm run build:client-and-server-bundles && npm run webpack:server",
"serve:ssr": "node dist/server.js",
"build:client-and-server-bundles": "ng build --prod && ng run angular.io-example:server",
"webpack:server": "webpack --config webpack.server.config.js --progress --colors"

有谁有主意,如何摆脱这个错误?

干得好

const domino = require('domino');
const fs = require('fs');
const template = fs.readFileSync(join(DIST_FOLDER , 'index.html')).toString();
const win = domino.createWindow(template);
global['window'] = win;
global['Node'] = win.Node;
global['navigator'] = win.navigator;
global['Event'] = win.Event;
global['KeyboardEvent'] = win.Event;
global['MouseEvent'] = win.Event;
global['Event']['prototype'] = win.Event.prototype;
global['document'] = win.document;

在行之前将上述代码放入server.ts文件中

const {AppServerModuleNgFactory, LAZY_MODULE_MAP} = require('./dist/server/main');

在server.ts中添加此行

global ['KeyboardEvent'] = null;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM