How to store the json value in session storage using angular2. I'm new to angular2 kindly do help.
below is my code:
.do(data => alert("All: " + alert(JSON.stringify(data))))
im getting json value in the alert. Now i need to store the value in session storage and it should be accessible in all over the application. Below is my code but is it not working.
var EmpDetails = [data => JSON.stringify(data)];
EmpDetails.push(JSON.parse(localStorage.getItem('session')));
localStorage.setItem('session', JSON.stringify(EmpDetails));
alert(EmpDetails);
Both localStorage and sessionStorage are part of web API which are used to store 'KEY' — 'VALUE' pairs in Angular. Both of them have same APIs and are easy to use. Both of them can be accessed by client side only and server doesn't have access and thus eliminate the security threat cookies present.
Using Observable Store with AngularCreate an Angular application using the Angular CLI or another option. In the constructor add a call to super() . The store allows you to turn tracking of store state changes on and off using the trackStateHistory property. See a list of Observable Store Settings.
SessionStorage is used for storing data on the client side. Maximum limit of data saving in SessionStorage is about 5 MB.
localStorage and sessionStorage are almost identical and have the same API. The difference is that with sessionStorage , the data is persisted only until the window or tab is closed. With localStorage , the data is persisted until the user manually clears the browser cache or until your web app clears the data.
You can store data to session or local storage. You can store both string and array into session
Store in to Session:
Use: import {SessionStorageService} from '../../sessionstorage.service';
Store string in session
let key: 'title';
let value: 'session';
sessionStorage.setItem(key, value);
Store array in session
let key: 'user';
let value: [{'name':'any-name','email':'[email protected]'}];
value = JSON.stringify(value);
sessionStorage.setItem(key, value);
Get stored session from sessionStorage by key
const session = sessionStorage.getItem('key');
Delete saved session from sessionStorage by key
sessionStorage.removeItem('key');
Delete all saved sessions from sessionStorage
sessionStorage.clear();
Store in to localStorage
You can store both string and array into location storage
Use: import {LocalStorageService} from "../../localstorage.service";
Store string in local storage
let key: 'title';
let value: 'session';
localStorage.setItem(key, value);
Store array in local storage
let key: 'user';
let value: [{'name':'any-name','email':'[email protected]'}];
value = JSON.stringify(value);
localStorage.setItem(key, value);
Get stored items from localStorage by key
const item= localStorage .getItem('key');
Delete saved session from localStorage by key
localStorage.removeItem('key'
);
Delete all saved items from localStorage
localStorage.clear();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With