问题解决方法:此错误是因为在进行Angular的SSR时,可能会遇到在isPlatformBrowser或isPlatformServer时使用了对象来检查平台类型,但是这些对象尚不存在。解决办法是,在检查平台类型之前,先确保这些对象已经存在。代码示例如下:
import { PLATFORM_ID } from '@angular/core'; import { isPlatformBrowser, isPlatformServer } from '@angular/common';
@Component({ ... }) export class ExampleComponent implements OnInit {
constructor(@Inject(PLATFORM_ID) private platformId: Object) {}
ngOnInit() { if (isPlatformBrowser(this.platformId)) { // Code for Browser platform } if (isPlatformServer(this.platformId)) { // Code for Server platform } } }