web services - User specific variables in Angular 2 -
i new angular , have been reading around still cannot find proper way handle user specific variables (let's username) in proper way. is, these variables can accessed , edited component (i trying avoid global variables since not think appropriate concrete case)
you can use browser local storage store userspecific data username/userprofile/authtoken etc
let userprofile = {username: 'abc', ...}; localstorage.setitem('userlocalprofile', userprofile);
you can remove well
localstorage.removeitem('userlocalprofile');
you probable write service , inject angular component purpose better suite need.
here example started..
import { injectable } '@angular/core'; @injectable() export class tokenservice { constructor() { } private storagename: string = "userlocalstorage"; settoken(token: string) { localstorage.setitem(this.storagename, token); } gettoken() { let token = localstorage.getitem(this.storagename); return token; } cleartoken() { localstorage.removeitem(this.storagename); } }
Comments
Post a Comment