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.
34 lines
670 B
TypeScript
34 lines
670 B
TypeScript
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();
|
|
}
|
|
}
|
|
}
|