Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Concept of shared preference in ionic

Tags:

ionic3

Does anyone know, how the concept of shared preference in android, is used in ionic?. I tried a lot but couldn't understood how it is used in ionic.

like image 378
devu mani Avatar asked Sep 17 '18 16:09

devu mani


1 Answers

In ionic we are use ionic storage: storage doc

You can install this plugin using following command

ionic cordova plugin add cordova-sqlite-storage

npm install --save @ionic/storage

Include it in your app.module.ts imports:

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

    // Inside of your @NgModule
    imports: [
        IonicStorageModule.forRoot() // Add this
    ],

Then, on the page you're using it on, reference it like this:

    constructor(private storage: Storage) { }

      ...

      // set a key/value
      storage.set('name', 'Max');

  // Or to get a key/value pair its key value can get from any page after settting key value
      storage.get('name').then((val) => {
        console.log('Your age is', val);
      });
    }

Also you can also use localforage: enter link description here

Installation localforage:

npm install localforage

Uses: injecting

import * as localforage from "localforage";

Store value

 localForage.setItem('key', 'value');

Getting value (same page or other page)

localforage.getItem('key');

Hope you will be helpful

like image 141
Utpaul Avatar answered Sep 28 '22 06:09

Utpaul