How to use Ionic 2 local storage?

Hi,
I am using Ionic 2 .How to save my form data in local storage and call that stored value in all pages.for example i save user name in local storage .how to i get the user name local storage value in all other pages.
If any working Examples?
Thank you.

Im using Native Storage instead Local Storage:

Store Value:

  NativeStorage.setItem('loginname', "MyName")
  .then(() => console.log('Stored Login Data!'), error => console.error('Error storing LoginData', error));

Get Value:

NativeStorage.getItem('loginname').then( data => this.name = data, console.error('Error getting LoginData', error));

How to import:
import { NativeStorage } from 'ionic-native';

Thank you so much for your reply. Once we save in local how can i call that stored value in another page.
Example: I registered user name and age in registration page and how to call that same value in profile page?

Did you read my comment?

I said:

Get Value:

NativeStorage.getItem('loginname').then( data => this.name = data, console.error('Error getting LoginData', error));

Thank you @lukasrein97

So in Login Page Store it like this:

 NativeStorage.setItem('loginname', "MyName")
  .then(() => console.log('Stored Login Data!'), error => console.error('Error storing LoginData', error));

 NativeStorage.setItem('loginpassword', "MyPassword")
  .then(() => console.log('Stored Login Data!'), error => console.error('Error storing LoginData', error));

and in Profile Page get it like this:

name: string;
password: string;

constructor{
NativeStorage.getItem('loginname').then( data => this.name = data, console.error('Error getting LoginData', error));
NativeStorage.getItem('loginpassword).then( data => this.password = data, console.error('Error getting LoginData', error));
}

Thank you @lukasrein97 for share working live examples code .

1 Like

I’m a bit surprised that those examples worked. They shouldn’t with ionic-native v3. Instead of calling static class methods, you need to inject instances and call object methods instead now.