How to use localStorage in RC0


#1

i am upgrading my app from previous ionic beta release , but cannot find how to do local storage in ionic 2 rc0.
anyone of you tried it?..please provide syntax if possible.
Thanks in advance


#2

You may want to use Ionic Storage instead of localStorage directly.


#3

Hi @riteshbhat17 ¿How are you doing? This happened to me as well.

I don’t know if you remember you used to this

import {Storage, LocalStorage} from 'ionic-angular';

then you would construct an instance of storage using LocalStorage as the implementation

private storage = new Storage(LocalStorage);

and then use it like this

this.storage.set('user', user);

Now with RC you can go like this

import {Storage} from '@ionic/storage';

private storage = new Storage();

and then this

this.storage.set('user', user);

As simple as that, Ionic will decide which is the best storage depending on where the app is running. I use chrome and Ionic uses IndexedDB instead of localstorage. And you must remember localstorage might not be a good place to write down important stuff not just for security reasons, as for device specific implementations of low disk space policies. in certain situations android or other apps may wipe localstorage, and if you rely on this data, app may have unexpected results.