error using isBrowser function in angular universal

前端 未结 1 1052
醉话见心
醉话见心 2021-01-07 02:25

I\'m trying to use the isBrowser function in angular universal but I keep getting the same error when I build. I did install the npm package of angular-universal

相关标签:
1条回答
  • 2021-01-07 03:03

    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.
        }
      }
    }
    
    0 讨论(0)
提交回复
热议问题