首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Ionic3学习笔记(七)Storage

Ionic3学习笔记(七)Storage

作者头像
Theo Tsao
发布2018-09-07 16:11:48
5970
发布2018-09-07 16:11:48
举报
文章被收录于专栏:Theo TsaoTheo Tsao

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

1. 安装

如果需要使用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 { }

2. 配置

配置存储引擎优先级,在 ./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 { }

3. 使用

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
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017-10-112,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1. 安装
  • 2. 配置
  • 3. 使用
相关产品与服务
对象存储
对象存储(Cloud Object Storage,COS)是由腾讯云推出的无目录层次结构、无数据格式限制,可容纳海量数据且支持 HTTP/HTTPS 协议访问的分布式存储服务。腾讯云 COS 的存储桶空间无容量上限,无需分区管理,适用于 CDN 数据分发、数据万象处理或大数据计算与分析的数据湖等多种场景。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档