这是一个angular4的项目,使用了服务端渲染,在引入了一个第三方的富文本编辑器——wangeditor后,出现了如下问题
_ua: navigator.userAgent,
^
ReferenceError: navigator is not defined
一般遇到这种不支持服务端渲染的部分,我都是使用官方推荐的方法,判断当前平台,然后决定是否执行,如下:
import { PLATFORM_ID } from '@angular/core';
import { isPlatformBrowser, isPlatformServer } from '@angular/common';
constructor(@Inject(PLATFORM_ID) private platformId: Object) { ... }
ngOnInit() {
if (isPlatformBrowser(this.platformId)) {
// Client only code.
...
}
if (isPlatformServer(this.platformId)) {
// Server only code.
...
}
}
但是这个方法对这个编辑器似乎没有啥作用,即时被旁路了,跑服务端渲染的时候也还是依旧报错,有没有大神能帮忙分析一下啥原因。。。谢谢各位!!
主模块里引入 BrowserModule 试试