花生酱
主 题
安装
npm install localforage
在vue工程中使用
引入
javascript
import localforage from 'localforage/dist/localforage.js'
创建多个实例仓库
javascript
import Vue from 'vue'
Vue.prototype.localforage = localforage
// 患者详情缓存数据库
const metaTemplateListDb = localforage.createInstance({
name: 'metaTemplateListDb',
storeName: `metaDataDbStore`
})
Vue.prototype.$metaTemplateListDb = metaTemplateListDb
// 字段树 缓存数据库
const treeTemplateListDb = localforage.createInstance({
name: 'treeTemplateListDb',
storeName: `treeDataDbStore`
})
Vue.prototype.$treeTemplateListDb = treeTemplateListDb
// 历史记录 缓存数据库
const historyRecordDb = localforage.createInstance({
name: 'historyRecordDb',
storeName: `historyRecordDbStore`
})
Vue.prototype.$historyRecordDb = historyRecordDb
使用
localforage
的getItem
和setItem
是支持Promise
语法的
javascript
async getDetailTemplateHandle(isSum) {
const metaTemplateListDb = this.$metaTemplateListDb
await metaTemplateListDb.setItem(`key`, [1]) // 设置
const res = await this.$metaTemplateListDb.getItem(`key`)// 获取
}
或
this.$metaTemplateListDb.getItem(`key`).then(res => {
console.log(res)
})
全部评论(0)