Angualr 14 ReferenceError: window is not defined

Viewed 28

I want to use angular universal in my project. Affter i added it I see error:

ReferenceError: window is not defined

I am not using window in my project, but maybe it uses in thirty part library. How to solve this problem?

I already saw simular qustion: Angular 9 Universal ReferenceError: window is not defined its answer doesn't help.

1 Answers

Encapsulate the code with a check using isPlatformServer

export class ArtistShowComponent {
  constructor(@Inject(PLATFORM_ID) private platformId: Object) {
    this.isServerSide = isPlatformServer(platformId);
  }

  isServerSide: boolean;

  doSomething() {
    // If the issue is in typescript
    if (this.isServerSide) {
      ...
    }
  }
}

If the issue is in a third-party angular component / HTML:

<!-- Remove the component from the DOM -->
<ng-carousel *ngIf="!isServerSide"></div>
Related