indexedDB使用方式之localForage

本文介绍了localForage,一个简化IndexedDB操作的库,具有大存储容量、异步处理、跨兼容性的优点。讲解了如何通过createInstance创建实例、存储和读取数据的方法,以及常见的操作如removeItem和clear。
摘要由CSDN通过智能技术生成

indexedDB使用方式之localForage

localForage是什么

localForage 是基于 indexedDB 封装的库,我们可以通过使用localForage,简化操作IndexedDB,方便使用。

优势

  1. 存储量大 ,百兆级,或者无限;
  2. 使用方便 ,无需序列化,异步方式;
  3. 兼容性好 ,默认使用IndexedDB,不支持会使用WebSQL或 localStorage;兼容性

使用方式

  1. 引入

    import localforage from 'localforage'
    
  2. 多实例

    var store = localforage.createInstance({
      name: "nameHere"
    });
    
    var otherStore = localforage.createInstance({
      name: "otherName"
    });
    
    // Setting the key on one of these doesn't affect the other.
    store.setItem("key1", {a:1});
    otherStore.setItem("key2", "123123");
    
  3. 读取

    store.getItem('key1').then(function (value) {
      // we got our value
    }).catch(function (err) {
      // we got an error
    });
    
    // 或者
    try {
        const value = await otherStore.getItem('key2');
        // This code runs once the value has been loaded
        // from the offline store.
        console.log(value);
    } catch (err) {
        // This code runs if there were any errors.
        console.log(err);
    }
    
  4. 其他:删除就是我们熟知的:removeItem,清空:clear

  5. 更多:中文API官方API

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值