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

I'm getting an error "ReferenceError: localStorage is not defined" in my angular project

Discussão em 'Angular' iniciado por Lucian Wu, Maio 20, 2025.

  1. Lucian Wu

    Lucian Wu Guest

    In my Angular project, I want to using a localStorage. But I get a error.

    Here is my code.

    const siteId = this.route.snapshot.queryParamMap.get('SiteId');
    if (siteId) {
    this.globalService.setLocalStorage('site_id',siteId);
    } else if (!localStorage.getItem("site_id")) {
    this.snackbarService.openSnackbar("Site id is missing", 'error');
    }


    Here is the error.


    ERROR ReferenceError: localStorage is not defined
    at inject (F:\companies\IOT-Billing\source\OnLineSignUp\src\app\modules\auth\login\login.component.ts:16:5)
    at callHookInternal (F:\companies\IOT-Billing\source\OnLineSignUp\.angular\cache\18.2.3\vite\deps_ssr\chunk-DH64UGMO.js:5640:10)
    at callHook (F:\companies\IOT-Billing\source\OnLineSignUp\.angular\cache\18.2.3\vite\deps_ssr\chunk-DH64UGMO.js:5655:7)
    at callHooks (F:\companies\IOT-Billing\source\OnLineSignUp\.angular\cache\18.2.3\vite\deps_ssr\chunk-DH64UGMO.js:5629:9)
    at executeInitAndCheckHooks (F:\companies\IOT-Billing\source\OnLineSignUp\.angular\cache\18.2.3\vite\deps_ssr\chunk-DH64UGMO.js:5598:5)
    at refreshView (F:\companies\IOT-Billing\source\OnLineSignUp\.angular\cache\18.2.3\vite\deps_ssr\chunk-DH64UGMO.js:10505:11)
    at detectChangesInView (F:\companies\IOT-Billing\source\OnLineSignUp\.angular\cache\18.2.3\vite\deps_ssr\chunk-DH64UGMO.js:10657:5)
    at detectChangesInViewIfAttached (F:\companies\IOT-Billing\source\OnLineSignUp\.angular\cache\18.2.3\vite\deps_ssr\chunk-DH64UGMO.js:10640:3)
    at detectChangesInEmbeddedViews (F:\companies\IOT-Billing\source\OnLineSignUp\.angular\cache\18.2.3\vite\deps_ssr\chunk-DH64UGMO.js:10616:7)
    at refreshView (F:\companies\IOT-Billing\source\OnLineSignUp\.angular\cache\18.2.3\vite\deps_ssr\chunk-DH64UGMO.js:10517:5)


    I want to store and get token in localStorage.

    Continue reading...

Compartilhe esta Página