1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

ERROR ReferenceError: sessionStorage is not defined Angular 18

Discussão em 'Angular' iniciado por Prabhas Kumra, Maio 23, 2025.

  1. I'm encountering an error in my Angular 18 application related to sessionStorage. My code is throwing a ReferenceError: sessionStorage is not defined in the MyComponent. Here’s the relevant code snippet where the error occurs:

    export class MyComponent implements OnInit {

    constructor() { }

    ngOnInit(): void {
    const sessionId = sessionStorage.getItem('sessionId'); // Error occurs here
    // Other logic
    }
    }


    Another interesting part is, I don't get this error when I use sesssionStorage.setitem

    export class GenerateInfo {
    public sessionId!: string;

    constructor() { }

    setSessionIdToStorage(): void {
    sessionStorage.setItem('sessionId', JSON.stringify(this.sessionId)); // No error here
    // Other logic
    }
    }

    Continue reading...

Compartilhe esta Página