this.debug 不是 Angular 通用函数

Gan*_*ong 7 express server-side-rendering angular-universal angular angular10

我正在使用 Angular 10 在我的项目中应用 SSR。

我发现很多人推荐使用domino.

下面是我的server.ts文件

...

import { existsSync, readFileSync } from 'fs';
import { createWindow } from 'domino';

const scripts = readFileSync('dist/video-website-angular/browser/index.html').toString();

const window = createWindow(scripts);
global['window'] = window;

import { AppServerModule } from './src/main.server';
import { APP_BASE_HREF } from '@angular/common';

...
Run Code Online (Sandbox Code Playgroud)

当我运行时npm run dev:ssr,我收到错误

mik*_*est 9

事实上,this.debug is not a function错误只是一个副作用。实际错误是第一个:

在此输入图像描述

为了修复它,您需要将 window 声明为 any,如下所示:

const window: any = createWindow(scripts);

// or via a cast

const window = createWindow(scripts) as any;
Run Code Online (Sandbox Code Playgroud)

还有另一种方法,我最不喜欢它,因为它实际上使 TS 代码表现得像 JS,并削减了所有类型和代码提示支持,但这里是:

(global as any).window = window;
(global as any).document = window.document;
(global as any).Event = window.Event;
(global as any).KeyboardEvent = window.KeyboardEvent;
Run Code Online (Sandbox Code Playgroud)

其中任何一个都应该可以解决您的问题。