You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
31 lines
666 B
TypeScript
31 lines
666 B
TypeScript
2 years ago
|
import {Inject, Injectable, PLATFORM_ID} from '@angular/core';
|
||
|
import {isPlatformBrowser} from '@angular/common';
|
||
|
|
||
|
@Injectable({
|
||
|
providedIn: 'root'
|
||
|
})
|
||
|
export class StorageService {
|
||
|
constructor(@Inject(PLATFORM_ID) private platformId: Object) {
|
||
|
}
|
||
|
|
||
|
getValue(key: string): any {
|
||
|
if (!isPlatformBrowser(this.platformId)) {
|
||
|
return null;
|
||
|
}
|
||
|
try {
|
||
|
return JSON.parse(localStorage.getItem(key));
|
||
|
} catch (e) {
|
||
|
return null;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
setValue(key: string, value: any): void {
|
||
|
if (isPlatformBrowser(this.platformId)) {
|
||
|
try {
|
||
|
localStorage.setItem(key, JSON.stringify(value));
|
||
|
} catch (e) {
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|