this.debug 不是函数 Angular 通用

this.debug is not a function Angular Universal

我在我的项目中使用 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';

...

当我 运行 npm run dev:ssr 时,我得到 error

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

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

const window: any = createWindow(scripts);

// or via a cast

const window = createWindow(scripts) as any;

还有另一种方法,我最不喜欢它,因为它实际上使 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;

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