Angular 17: localStorage is not defined Error After Saving Changes and Browser Stuck in...

Discussão em 'Angular' iniciado por Usama Ikram, Dezembro 25, 2024.

  1. Usama Ikram

    Usama Ikram Guest

    When i run project in angular 17 it runs successfully. However when i made some changes in code and save changes then:

    The browser page stucks in a reloading state

    Also this error shown up:

    Error during ngOnInit: ReferenceError: localStorage is not defined

    Here is my ngOnInit code block:

    async ngOnInit() {
    try {
    await this.fetchUsers();

    this.chatService.fetchMessages();
    this.chatService.messages.subscribe((messages) => {
    this.messages = messages;
    });

    const users = this.authService.getUsers();
    const { username, id } = this.authService.getUserNameFromToken() || {};
    this.name = username ?? '';
    console.log("Display token name:", this.name);
    let token = localStorage.getItem('authToken');
    console.log("Token: ",token);
    this.id = id ?? '';
    } catch (error) {
    console.error("Error during ngOnInit:", error);
    }

    }
    ```[![Here is image of browser page which stucks in loading state when i made changes in code and save them][1]][1]


    [1]: https://i.sstatic.net/4jvdngLj.png

    Continue reading...

Compartilhe esta Página