美文网首页
localForage的使用

localForage的使用

作者: INGME | 来源:发表于2023-11-07 11:17 被阅读0次
localForage 的介绍
localForage 是基于 indexedDB 封装的库,通过它的我们可以简化 indexedDB 的使用。

indexedDB

localForage 的使用
  1. 下载
npm install localforage

import localforage form 'localforage'
  1. 创建一个 indexedDB
const myIndexedDB = localforage.createInstance({
    name: 'myIndexedDB'
})
  1. 存值
myIndexedDB.setItem(key, value)
  1. 取值
由于indexedDB的存取值都是异步的,建议使用 promise.then() 或 async/await 去读取
myIndexedDB.getItem(key).then(value => {
    // we got our value
}).catch(err => {
    // we got on error
})

or

try {
  const value = await myIndexedDB.getItem(key);
  // This code runs once the value has been loaded
  // from the offline store.
} catch (err) {
  // This code runs if there were any errors.
}
  1. 删除某项
myIndexedDB.removeItem(key)
  1. 重置数据库
myIndexedDB.clear()

细节及其他使用方式请参考官方中文文档

相关文章

网友评论

      本文标题:localForage的使用

      本文链接:https://www.haomeiwen.com/subject/toffwdtx.html