Ionic2 Data Storage __ionic2

Source: Internet
Author: User
Tags export class sqlite

When in a native app lexical environment, the general priority is to use SQLite, because it is one of the most widely used stable document database, it also avoids some difficulties such as localstorage and INDEXEDDB, such as the system clears the data when the disk space is not enough.

If you are running on a Web or web app, you tend to use Indexeddb,websql and localstorage.

If you are using SQLite, first you have to install the Cordova-sqlite-storage plugin.

Cordova Plugin Add Cordova-sqlite-storage--save

NPM then follows the package:

NPM Install--save @ionic/storage

Then import the plugin and inject the module into the app in an array of your ngmodule imports.

Here's an example:

Import {Ionicstoragemodule} from ' @ionic/storage ';

@NgModule ({
  declarations: [
    //...
  ],
  imports: [
    ionicmodule.forroot (MYAPP),
    Ionicstoragemodule.forroot ()//Right here
  ],
  bootstrap: [Ionicapp],
  entrycomponents: [
    //...
  ],
  providers: []
})
export class Appmodule {}

This way, if you need to use the database in that component, you can inject it through the import:

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

Export class MyApp {
  Constructor (storage:storage) {

     storage.ready (). Then (() => {

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

       Or to get a key/value pair
       storage.get (' age '). Then ((val) => {
         console.log (' Your The Age is ', Val)
     }} );
  }
}

Configure Storage

Developers can configure the storage engine with a defined storage engine priority, or customize the configuration options to Localforage.

Note: Any custom configuration will be merged into the default configuration.

Import {Ionicstoragemodule} from ' @ionic/storage ';

@NgModule {
  declarations: ...,
  imports: [
    ionicstoragemodule.forroot ({
      name: ' __mydb ',
         Driverorder: [' indexeddb ', ' sqlite ', ' Websql ']
    }
  ],
  bootstrap: ...,
  entrycomponents: ...,
   providers: []
})
export class Appmodule {}

The following is how the database is used: Driver gets the driver name ready () reaction store get (key) gets the corresponding data set (Key,value) sets the data remove (key) deletes the corresponding data clear () Clears all data length () Get data length keys () Fetch key ForEach (callback) Traverse database

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.