Tec*_*thi 4 server-side-rendering angular-universal angular angular9
错误参考错误:文档未定义
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;Run Code Online (Sandbox Code Playgroud)
即使在 Server.ts 中添加这个修复问题但在性能 TTFB 时间太高。任何有解决方案...?
小智 6
这些全局变量包括 window、document、localStorage、indexedDB、setTimeout 和 setInterval,您不能在 Angular 通用应用程序中使用
使用 Anguar 公共模块中的文档对象
从库导入
import { DOCUMENT } from '@angular/common';
Run Code Online (Sandbox Code Playgroud)
注入服务
@Inject(DOCUMENT) private document: Document,
Run Code Online (Sandbox Code Playgroud)
小智 6
尝试使用@angular/common 包提供的 DOCUMENT 常量
import { Inject, Injectable } from '@angular/core';
import { DOCUMENT } from '@angular/common';
@Injectable()
export class MyService {
constructor(@Inject(DOCUMENT) private document: Document) {}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3894 次 |
| 最近记录: |