且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在 angular 通用中使用 isBrowser 函数时出错

更新时间:2022-06-19 09:25:36

看起来他们在 angular 通用示例中使用 isPlatformBrowser() 而不是 isBrowser().https://github.com/angular/universal#universal-gotchas

It looks like they are using isPlatformBrowser() instead of isBrowser() in the angular universal example. https://github.com/angular/universal#universal-gotchas

import { Component, Inject, PLATFORM_ID, OnInit } from '@angular/core';
import { isPlatformBrowser, isPlatformServer } from '@angular/common';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css'],
})
export class AppComponent implements {

  constructor(
    @Inject(PLATFORM_ID) private platformId: Object
  ){
  }

  ngOnInit(){
    if (isPlatformBrowser(this.platformId)) {
     //Client only code.
    } else {
     //Server only code.
    }
  }
}