Exclude component that breaks Angular Universal Exclude component that breaks Angular Universal typescript typescript

Exclude component that breaks Angular Universal


The fix is simple, you should use a PLATFORM_ID token together with the isPlatformBrowser or isPlatformServer method.

Inside your template use the #ngIf statement:

<section class="sec-space-b" id="banner" *ngIf="isBrowser">

And inside the component code initialize the isBrowser field as:

import { isPlatformBrowser } from '@angular/common';import { Component, OnInit, Inject, PLATFORM_ID } from '@angular/core';@Component({  selector: 'app-home-banner',  templateUrl: './banner.component.html',  styleUrls: ['./banner.component.scss']})export class BannerComponent implements OnInit {  public isBrowser = isPlatformBrowser(this.platformId);  constructor(@Inject(PLATFORM_ID) private platformId: any) { }}

You can read more about isPlatformServer and isPlatformBrowser in this article (they are used there):https://www.twilio.com/blog/create-search-engine-friendly-internationalized-web-apps-angular-universal-ngx-translate

You can also check out my talk about Angular Universal (13:26 - about running different code on browser and server):https://www.youtube.com/watch?v=J42mqpVsg0k