1

This issue occurs in Angular 7. I have upgraded application from angular 6 to angular 7

Getting this error:

 var user_id = window.localStorage.getItem('user_id');
              ^

ReferenceError: window is not defined
    at Object../src/app/global_variable.ts (D:\Project\public\dist\server.js:208477:15)
    at __webpack_require__ (D:\Project\public\dist\server.js:169701:30)
    at Object../src/app/services/auth.service.ts
1
  • @Kingslayer omit does not work, as now localStorage is not defined Commented Mar 29, 2019 at 10:00

4 Answers 4

1

Universal toolkit renders code on the Server Side and window object is only available in browsers that is why you are getting this error.

You can add condition to execute client side code only on browser by using isPlatformBrowser module.

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

@Component({
  selector: "app-root",
  templateUrl: "./app.component.html"
})
export class AppComponent implements OnInit {

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

   ngOnInit() {

     // Client only code.
     if (isPlatformBrowser(this.platformId)) {
        // write your client side code here
     }

   }
 }
Sign up to request clarification or add additional context in comments.

Comments

0

"window" is the global object in Javascript, so you can omit it if there's no chance for conflict. Try using this,

var user_id = localStorage.getItem('user_id');

2 Comments

By using solution I gave above
omit does not work, as now localStorage is not defined @Kingslayer
0

When you add angular universal in your app window, document, navigator, and other browser types - do not exist on the server - so using them, or any library that uses them (jQuery for example) will not work.

If you need to use them, consider limiting them to only your client and wrapping them situationally. You can use the Object injected using the PLATFORM_ID token to check whether the current platform is browser or server.

in My Project i have following code inside component ts file and it is working

import { WINDOW } from '@ng-toolkit/universal';
import { Component, Inject, PLATFORM_ID } from '@angular/core';
import { isPlatformBrowser } from '@angular/common';


@Component({
 selector: 'app-root',
 templateUrl: './app.component.html',
 styleUrls: ['./app.component.scss']
 })

  export class AppComponent {
  title = 'app';

  constructor(@Inject(WINDOW) public window: Window,
    @Inject(PLATFORM_ID) private platformId: Object) {
  }

  onActivate(event) {
    if (isPlatformBrowser(this.platformId)) {
      this.window.scroll(0, 0);  // window object used which is Instance of Window 

    }
  }

 }

Comments

0

This works for me:

import { Component, AfterViewInit } from '@angular/core';

export class HeaderComponent implements AfterViewInit {
  window!: Window & typeof globalThis;
  
  ngAfterViewInit(): void {
    if (this.window) {
      if (window.scrollY > 0) {
        this.isScrolled = true;
      };
    };
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.