https://blog.csdn.net/CEZLZ/article/details/108147697
https://github.com/RobinCK/vue-ls
// main.js
/*
引入-----本地存储插件
*/
import Storage from 'vue-ls';
const storageOptions = {
namespace: '', // key 键的前缀
name: 'ls', // 变量名称,使用方式:Vue.变量名称 或 this.$变量名称
storage: 'local', // 存储名称: local, session, memory
};
Vue.use(Storage, storageOptions);
使用方法
// name:要获取的 key
// def:默认为 null。如果 key 不存在,则返回 def
this.$ls.get(name, def)
// 设置的 key 名
// 设置的 key 的 value 值
// 设置过期时间,单位毫秒
this.$ls.set(name, value, expire)
// 删除 key,成功返回 true,否则返回 false
this.$ls.remove(name)
// 清空所有 key
this.$ls.clear()
// 设置侦听器,监听 key,若发生变化时,就会触发回调函数 callback
this.$ls.on(name, callback)
callback 三个参数:
newValue: in storage 的当前值name,从持久化的 JSON 中解析
oldValue: in storage 的旧值name,从持久化的 JSON 中解析
url: 修改来源标签的 url
// 删除设置的侦听器
this.$ls.off(name, callback)
还有一个插件 vue-web-storage [https://github.com/ankurk91/vue-web-storage]
网友评论