Created
September 1, 2019 21:55
-
-
Save khalidsheet/dc6017a32acfa31c5d90b638652f3625 to your computer and use it in GitHub Desktop.
Simple Session Manager Service in angular
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Injectable } from "@angular/core"; | |
@Injectable({ | |
providedIn: "root" | |
}) | |
export class SessionManagerService { | |
constructor() {} | |
set(key: string, value: any): SessionManagerService { | |
if (value instanceof Object) { | |
value = JSON.stringify(value); | |
} | |
localStorage.setItem(key, value); | |
return this; | |
} | |
get(key: string): string { | |
return localStorage.getItem(key); | |
} | |
forget(key: string): void { | |
localStorage.removeItem(key); | |
} | |
destroy(): void { | |
localStorage.clear(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment