JS 自带的本地存储很蛋疼的,不管是 cookie、sessionStorage 还是 localStorage 都只能存储字符串,
通常会将数据整理成一个对象, JSON.stringify 转一下存入, 获取时再 JSON.parse,
同时原生的方法还稍稍微微有些兼容问题。

localForage

不局限于存储字符串,可以存储数字、对象、数组、Blob 等类型。
优雅降级策略,若浏览器不支持 IndexedDB 或 WebSQL,则使用 localStorage。
异步读取,提供了 callback 和 Promise 两种异步处理形式。

github

使用方法

看官方文档吧,很简单
中文版文档如下

vlf

localForage vue 的封装

github

使用文档

import Vlf from 'vlf'
import localforage from 'localforage'
Vue.use(Vlf, localforage)
// 创建实例
this.$vlf.createInstance({
    storeName: 'user'
})
// 迭代
this.$vlf.iterate((value, key, num) => {
    console.log(key);
});
// 设置值
this.$vlf.setItem('test', 'hello').then(v => {
    console.log(v);
});

// ...和官方调用一致
// The other methods of use are the same as the official website, just add a this.$vlf in front, the same behind!