角度通用错误ReferenceError:未定义MouseEvent

时间:2019-10-18 07:38:35

标签: javascript node.js angular typescript runtime-error

我正在使用Angle Universal渲染ssr,同时使用angular cli命令:npm run build:ssr && npm run serve:ssr,我遇到了错误。

使用Angular8

/home/xyz/projects/my-app/dist/server/main.js:139925
Object(tslib__WEBPACK_IMPORTED_MODULE_0__["__metadata"])("design:paramtypes", [MouseEvent, HTMLElement]),
                                                                               ^

ReferenceError: MouseEvent is not defined
    at Module.UPO+ (/home/xyz/projects/my-app/dist/server/main.js:139925:84)

npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! patient-app@0.0.0 serve:ssr: `node dist/server`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the patient-app@0.0.0 serve:ssr script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/vijay/.npm/_logs/2019-10-18T07_30_11_791Z-debug.log

1 个答案:

答案 0 :(得分:0)

在您的角度应用程序的通用模式下运行时,DocumentWindowlocalstorageMouseEvent等客户端代码将不存在,因为您的第一页将是在服务器上渲染。

因此,如果您的代码中存在任何此类代码,则需要像这样将客户端代码包装在platformBrowser中-

import { ..., PLATFORM_ID, ... } from '@angular/core';
import { isPlatformBrowser } from '@angular/common';


constructor(
    @Inject(PLATFORM_ID) private platformId: Object,
){
    if (isPlatformBrowser(this.platformId)) {
       // MouseEvent code
    }
}