Skip to content

Instantly share code, notes, and snippets.

@alfredfrancis
Created July 6, 2017 06:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alfredfrancis/a8b4d36c776302c9d86cb90723394027 to your computer and use it in GitHub Desktop.
Save alfredfrancis/a8b4d36c776302c9d86cb90723394027 to your computer and use it in GitHub Desktop.
Angular 4 - Persist & retrive variables over session
persist(key,value){
/*
* Store user information in sessionStorage
* @param {string} key - key name
* @param {json} value - value
* @return {}
*/
if(typeof value == "object"){
return sessionStorage.setItem(key,JSON.stringify(value))
}else{
return new Error("Oops! Only Objects can be persisted.")
}
}
retrive(key){
/*
* Store user information in sessionStorage
* @param {string} key - key name
* @return {json}
*/
return JSON.parse(sessionStorage.getItem(key))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment