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

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

  • 相关阅读:
    利用相关的Aware接口
    java 值传递和引用传递。
    权限控制框架Spring Security 和Shiro 的总结
    优秀代码养成
    Servlet 基础知识
    leetcode 501. Find Mode in Binary Search Tree
    leetcode 530. Minimum Absolute Difference in BST
    leetcode 543. Diameter of Binary Tree
    leetcode 551. Student Attendance Record I
    leetcode 563. Binary Tree Tilt
  • 原文地址:https://www.cnblogs.com/metaphors/p/7666013.html
Copyright © 2011-2022 走看看