import {Inject, Injectable, PLATFORM_ID} from "@angular/core"; import {BehaviorSubject, Observable} from "rxjs"; import {isPlatformBrowser} from "@angular/common"; @Injectable({ providedIn: "root" }) export class LocalStorageService { private communityDirectLink: BehaviorSubject; constructor(@Inject(PLATFORM_ID) private platformId: string) { if(isPlatformBrowser(this.platformId)) { let item = localStorage.getItem('directLink'); if(item !== null) { this.communityDirectLink = new BehaviorSubject(item == 'true'); } else { this.communityDirectLink = new BehaviorSubject(false); } } else this.communityDirectLink = new BehaviorSubject(true); } public setCommunityDirectLink(value: string) { this.communityDirectLink.next(Boolean(value).valueOf()); localStorage.setItem('directLink', value); } public get(): Observable { return this.communityDirectLink.asObservable(); } }