Bom dia, toda ves que preciso usar o localStorage o Angular me apresenta a a seguinte mensagem:
ERROR ReferenceError: localStorage is not defined
at _TokenService.retornaToken (/Users/leonardocarreradasilva/Git/eventosV2AG/src/app/services/token/token.service.ts:19:5)
at _TokenService.possuiToken (/Users/leonardocarreradasilva/Git/eventosV2AG/src/app/services/token/token.service.ts:23:19)
at _UserService (/Users/leonardocarreradasilva/Git/eventosV2AG/src/app/services/user/user.service.ts:15:23)
at Object.UserService_Factory (/Users/leonardocarreradasilva/Git/eventosV2AG/src/app/services/user/user.service.ts:42:3)
at eval (/Users/leonardocarreradasilva/Git/eventosV2AG/node_modules/@angular/core/fesm2022/core.mjs:5239:43)
at runInInjectorProfilerContext (/Users/leonardocarreradasilva/Git/eventosV2AG/node_modules/@angular/core/fesm2022/core.mjs:3644:9)
at R3Injector.hydrate (/Users/leonardocarreradasilva/Git/eventosV2AG/node_modules/@angular/core/fesm2022/core.mjs:5238:17)
at R3Injector.get (/Users/leonardocarreradasilva/Git/eventosV2AG/node_modules/@angular/core/fesm2022/core.mjs:5106:33)
at injectInjectorOnly (/Users/leonardocarreradasilva/Git/eventosV2AG/node_modules/@angular/core/fesm2022/core.mjs:3831:40)
at Module.ɵɵinject (/Users/leonardocarreradasilva/Git/eventosV2AG/node_modules/@angular/core/fesm2022/core.mjs:3837:42)
meu Token.service.ts
import { Inject, Injectable } from '@angular/core';
const KEY = 'token';
@Injectable({
providedIn: 'root',
})
export class TokenService {
salvarToken(token: string) {
localStorage.setItem(KEY, token);
}
excluiToken() {
localStorage.removeItem(KEY);
}
retornaToken() {
return localStorage.getItem(KEY) ?? '';
}
possuiToken() {
return !!this.retornaToken();
}
}
Alguem ja passou por isso ?