这个问题是因为Angular Universal在服务器端渲染时没有window对象可用,而CKEditor5依赖于浏览器环境中的window对象。解决方法是在使用CKEditor5的代码中判断是否在浏览器环境中运行,如果是则执行相关代码。
以下是一个可能的解决方法:
在你的组件中,使用isPlatformBrowser方法来判断是否在浏览器环境中运行:
import { Component, OnInit, Inject, PLATFORM_ID } from '@angular/core';
import { isPlatformBrowser } from '@angular/common';
@Component({
selector: 'app-editor',
templateUrl: './editor.component.html',
styleUrls: ['./editor.component.css']
})
export class EditorComponent implements OnInit {
constructor(@Inject(PLATFORM_ID) private platformId: Object) { }
ngOnInit() {
if (isPlatformBrowser(this.platformId)) {
// 在浏览器环境下执行的代码
// 初始化CKEditor5等相关操作
// 例如:window.CKEditor = require('@ckeditor/ckeditor5-build-classic');
}
}
}
通过使用isPlatformBrowser方法和PLATFORM_ID令牌,在浏览器环境中运行相关代码,在服务器端渲染时忽略相关代码。这样可以避免“window is not defined”错误。
请注意,上述代码中的CKEditor5初始化代码是一个示例,你需要根据你的实际情况进行相应的修改和配置。
希望能帮助到你!