Where the fuck is LocalStorage in AngularJS 2?!

So I’m a new migrator from AngularJS1/Ionic1 to AngularJS2/Ionic 2.

Already I’m working on a new production project while learning on the job, and so far so good, until I ran into my post login processes, where I needed to store the logged in user info in the app, which traditionally would be done with LocalStorage, built in, in Ionic Framework 1.

To my dismay, there are ZERO docs on localStorage in AngularJS 2/Ionic Framework 1, nor the built in Storage service it provides. I managed to only learn about Storage-LocalStorage from a Udemy tutorial and searches, and turns out the way to import the service was wrong in most of the results.

The above syntax will result in an error that says:

Turns out, LocalStorage was removed entirely in Ionic2, and Storage was separated away from the core ionic-angular package, into a separate package called ionic/storage.

One has to install the @ionic/storage package first, by running:

Followed by importing the service in Ionic Framework 2 final, is this:

Remember to do this both in your component and app.module.ts. Remember to also declare the provider in your app.module.ts as so:

So to set and get data from Storage, simply do this:

Set

Get

Clear

 

You’re welcome.

 

 

No Comments

Cancel