Storage可以很容易的存储键值对和JSON对象。Storage在底层使用多种存储引擎,根据运行平台选择最佳的存储方式。 当运行在Native模式时,Storage将优先使用SQLite。 当运行在Web中或作为PWA应用时,Storage将根据你确定的优先级使用IndexedDB、WebSQL或localstorage。
如果需要使用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 { }
配置存储引擎优先级,在 ./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 { }
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;
});
}
}
更多可详见
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有