我想在indexedDB中使用两个表,但是inoic 3文档目前还不是很好。
在我导入了IonicStorageModule的app.modules中
IonicStorageModule.forRoot()
然后在我的storage-uitls.ts文件中,我试图在构造函数中生成两个表,就像提到的那样here
constructor(private storage: Storage) {
console.log("---------------")
this.gpsData = new Storage({
name: '__my_custom_db',
storeName: '_contacts',
driverOrder: ['indexeddb', 'sqlite', 'websql'],
});
this.serviceData = new Storage({
name: '__my_custom_db',
storeName: '_media',
driverOrder: ['indexeddb', 'sqlite', 'websql'],
});
}
但是这不起作用。我只是在indexedDB中获得了默认的生成表。
任何人都知道如何生成自己的表并对其进行访问吗?
我对IONIC 4项目的设置(我没有在IONIC 3项目中尝试过:]
import { Storage } from '@ionic/storage';
@Component({
selector: 'app-tab1',
templateUrl: 'tab1.page.html',
styleUrls: ['tab1.page.scss']
})
export class Tab1Page implements OnInit {
private newReleasesDB: any;
constructor() {
this.newReleasesDB = new Storage({
name: '__myCustom_db',
storeName: '_newReleases',
driverOrder: ['sqlite', 'indexeddb', 'websql', 'localstorage']
});
}
ngOnInit() {
// get saved records from Storage
this.newReleasesDB.get('keyname').then((data: any) => {
// do stuff with the data in storage
});
}
}
这可用于同一数据库中的多个存储,例如:
import { Storage } from '@ionic/storage';
@Component({
selector: 'app-tab1',
templateUrl: 'tab1.page.html',
styleUrls: ['tab1.page.scss']
})
export class Tab1Page implements OnInit {
private newReleasesDB: any;
private myFrendsDB: any;
constructor() {
this.newReleasesDB = new Storage({
name: '__myCustom_db',
storeName: '_newReleases',
driverOrder: ['sqlite', 'indexeddb', 'websql', 'localstorage']
});
this.myFrendsDB = new Storage({
name: '__myCustom_db',
storeName: '_myFriends',
driverOrder: ['sqlite', 'indexeddb', 'websql', 'localstorage']
});
}
ngOnInit() {
// get saved records from Storage
this.newReleasesDB.get('keyname').then((data: any) => {
// do stuff with the data in storage
});
// get saved friends from Storage
this.myFrendsDB.get('keyname').then((data: any) => {
// do stuff with the data in storage
});
}
}
[如果遇到问题,请尝试清除存储并再次运行脚本。