My project was running perfectly but when i implement universal then i am getting \"window is not defined\".
My error is as below.
node_modules/hamm
As @trichetriche said. You don't have access to window
object on the server side. The only thing which should be mentioned here is, that the better way instead of using
if(window) {}
Would be (let's say it is more "Angular" ;) ):
import { isPlatformBrowser, isPlatformServer } from '@angular/common';
import { Inject, PLATFORM_ID } from '@angular/core';
constructor(@Inject(PLATFORM_ID) private platformId: any) {}
public someMethod(): boolean {
if (isPlatformBrowser(this.platformId)) {
//action specific for browser
}
}
You can take a look at live example in one of my repositories: https://github.com/maciejtreder/angular-universal-pwa/blob/master/src/app/services/notification.service.ts