gpt4 book ai didi

Angular 9 SSR 构建服务错误——错误引用错误 : document is not defined

转载 作者:行者123 更新时间:2023-12-04 12:11:36 25 4
gpt4 key购买 nike

错误引用错误:文档未定义

import { readFileSync } from 'fs';
const domino = require('domino'); // import the library `domino`
const DIST_FOLDER = join(process.cwd(), 'dist/browser');
const template = readFileSync(join(DIST_FOLDER, 'index.html')).toString(); // use `index.html` as template
const win = domino.createWindow(template); // create object Window
global['window'] = win;
global['Event'] = win.Event; // assign the `win.Event` to prop `Event`
global['document'] = win.document;


即使在 Server.ts 中添加这个修复问题但在性能 TTFB 时间太高。
任何有解决方案...?

最佳答案

尝试使用@angular/common 包提供的 DOCUMENT 常量

import { Inject, Injectable } from '@angular/core';
import { DOCUMENT } from '@angular/common';

@Injectable()
export class MyService {
constructor(@Inject(DOCUMENT) private document: Document) {}
}

关于Angular 9 SSR 构建服务错误——错误引用错误 : document is not defined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60636286/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com