我有一个角13应用程序,使用@auth0/auth0-angular。当我尝试实现SSR时,它会抛出没有定义窗口的错误。我在server.ts中添加了以下内容
const domino = require('domino');
const fs = require('fs');
const path = require('path');
const template = fs.readFileSync(path.join(__dirname, '..', 'browser', 'index.html')).toString();
const win = domino.createWindow(template);
global['window'] = win;
global['document'] = win.document;这解决了一些问题,但是经过几个小时的故障排除后,我才知道其余的错误都是由Auth0,@auth0/auth0-angular提出的。
我在@auth0/auth0-angular文档中阅读这,在angular-universal docs中阅读这。我现在明白了这个问题,并且有了一个解决办法,这已经得到了两个库的认可,但是我觉得很难实现。我想知道是否有相同的示例实现,是否有人已经这样做过呢?
任何帮助都是非常感谢的。在期待中感谢你们。
发布于 2022-10-07 08:08:38
我面对同样的问题,花了一些时间深入研究,以下是可能的解决方案。
将这些更改应用于app.server.module:
import * as crypto from 'crypto';
import { createWindow } from 'domino';
import * as fs from 'fs';
if (!global.window) {
const template = fs.readFileSync('./src/index.html').toString();
const win = createWindow(template);
(global as any).window = win;
(global as any).window.crypto = crypto;
(global as any).window.isMobileOnServer = true;
(global as any).document = win.document;
(global as any).navigator = win.navigator;
(global as any).location = win.location;
}还添加了提供程序
{provide: AuthService, useValue: {}https://stackoverflow.com/questions/72012878
复制相似问题