27 lines
797 B
TypeScript
27 lines
797 B
TypeScript
// Angular
|
|
import { isPlatformBrowser } from '@angular/common';
|
|
import { Inject, Injectable, PLATFORM_ID } from '@angular/core';
|
|
|
|
@Injectable({ providedIn: 'root' })
|
|
export class LocalStorageService {
|
|
constructor(@Inject(PLATFORM_ID) public platformId: Object) {}
|
|
|
|
getItem(tag: string) {
|
|
if (isPlatformBrowser(this.platformId)) {
|
|
const value = (window as any).localStorage.getItem(tag);
|
|
if (value) return JSON.parse(value);
|
|
}
|
|
return undefined;
|
|
}
|
|
|
|
setItem(tag: string, value: any) {
|
|
if (isPlatformBrowser(this.platformId))
|
|
(window as Window).localStorage.setItem(tag, JSON.stringify(value));
|
|
}
|
|
|
|
deleteItem(tag: string) {
|
|
if (isPlatformBrowser(this.platformId))
|
|
(window as Window).localStorage.removeItem(tag);
|
|
}
|
|
}
|