import { isPlatformBrowser } from '@angular/common'; import { Inject, Injectable, PLATFORM_ID } from '@angular/core'; @Injectable({ providedIn: 'root' }) export class StorageService { private isBrowser: boolean = false; constructor(@Inject(PLATFORM_ID) platformId: object) { this.isBrowser = isPlatformBrowser(platformId); } getItem(key: string): string | null { if (this.isBrowser) { return localStorage.getItem(key); } return null; } setItem(key: string, value: string) { if (this.isBrowser) { localStorage.setItem(key, value); } } clear() { if (this.isBrowser) { localStorage.clear(); } } }