【问题标题】:this.debug is not a function Angular Universalthis.debug 不是 Angular Universal 的函数
【发布时间】:2021-01-31 05:20:14
【问题描述】:

我正在使用 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

【问题讨论】:

    标签: angular express server-side-rendering angular-universal angular10


    【解决方案1】:

    其实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;
    

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

    【讨论】:

    • 我的global['window'] 已经分配,​​只是现在突然开始出现此错误。我假设第 3 方库有条件地依赖于this.debug,但现在不知道为什么。我所做的只是切换分支并执行npm i。即使在删除 node_modules 之后,切换回并运行 npm i 也无济于事。我被搞砸了。
    • 有时切换分支可能会弄乱 node_modules。您可以尝试运行rm -frd node_modules,然后再次运行npm i。希望它能为您解决。
    • @mikeandtherest,你知道如何防止编译器在出现此类错误时退出吗?
    • 我在这里添加了问题stackoverflow.com/questions/68151309/…
    猜你喜欢
    • 2018-12-10
    • 2023-03-03
    • 1970-01-01
    • 2022-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多