zoukankan      html  css  js  c++  java
  • Ionic3学习笔记(七)Storage

    本文为原创文章,转载请标明出处

    目录

    1. 简介
    2. 安装
    3. 配置
    4. 使用

    1. 简介

    Storage可以很容易的存储键值对和JSON对象。Storage在底层使用多种存储引擎,根据运行平台选择最佳的存储方式。
    当运行在Native模式时,Storage将优先使用SQLite。
    当运行在Web中或作为PWA应用时,Storage将根据你确定的优先级使用IndexedDB、WebSQL或localstorage。

    2. 安装

    如果需要使用SQLite,先安装 Cordova-sqlite-storage ,命令行输入

    ionic cordova plugin add cordova-sqlite-storage
    npm install --save @ionic-native/sqlite
    

    ./src/app/app.module.ts 中添加

    import { IonicStorageModule } from '@ionic/storage';
    
    @NgModule({
      declarations: [...],
      imports: [
        ...,
        IonicStorageModule.forRoot()
      ],
      bootstrap: [...],
      entryComponents: [...],
      providers: [...]
    })
    
    export class AppModule { }
    

    3. 配置

    配置存储引擎优先级,在 ./src/app/app.module.ts 中添加

    import { IonicStorageModule } from '@ionic/storage';
    
    @NgModule({
      declarations: [...],
      imports: [
        ...,
        IonicStorageModule.forRoot({
          name: 'myApp',
          driverOrder: ['sqlite', 'indexeddb', 'websql']
        })
      ],
      bootstrap: [...],
      entryComponents: [...],
      providers: [...]
    })
    
    export class AppModule { }
    

    4. 使用

    import {Injectable} from '@angular/core';
    
    import {Storage} from '@ionic/storage';
    
    @Injectable()
    export class UserData {
    
      constructor(public storage: Storage) {
      }
    
      setUsername(username: string): void {
        this.storage.set('username', username);
      }
    
      getUsername(): Promise<string> {
        return this.storage.get('username').then((value) => {
          return value;
        });
      }
      
    }
    

    更多可详见

    1. Ionic Storage
    2. GitHub - localForage

    如有不当之处,请予指正,谢谢~

  • 相关阅读:
    Sublime Text 2 && MinGW G++ On Windows
    [zz]linux IO(function open read write close)
    PHP的基本常识(1)
    helloworld.c 的一次系统旅行(1) 读书笔记
    PHP 仿博客园 个人博客(1)
    配置 .htaccess 单点入口
    仿博客园个人博客(3)基本完成
    PHP的基本常识(2)
    JQuery 获得所有表单值
    PHP 仿博客园 个人博客(2)
  • 原文地址:https://www.cnblogs.com/metaphors/p/7666013.html
Copyright © 2011-2022 走看看