因为项目中常需用到本地storage存储,复杂的数组、对象等,每次都须要JSON.stringify()
和JSON.parse()
进行序列化、反序列化操做,以为有点麻烦,再者,存入storage的数值,取出来就变成字符串了,所以,对storage简单封装了一下,使取出的值类型和存入时保持一致,也能够存入整个object对象。git
一、安装github
npm install pz_storage --save
复制代码
二、使用npm
import storage from 'pz_storage';
// 存储单个值
storage.set('name', 'esther');
// 获取单个值
storage.get('name');
// 删除单个值
storage.remove('name');
// 清空所有
storage.clear();
// 存储对象,key - value形式
storage.setList({
a: '1',
b: '2.1',
c: ['a', 'b', 'c'],
d: {
'd-1': 'd-1',
'd-2': 'd-2'
},
e: true,
f: new Date(),
g: function(){
console.log(111);
}
});
// 获取多个值,传入数组形式的key
storage.getList(['a', 'b', 'd', 'f']);
// 删除多个值,传入数组形式的key
storage.removeList(['a', 'b', 'd', 'f'])
// sessionStorage, api同上
storage.session.set('name', 'esther');
复制代码
也可直接在HTML页面引入dist文件夹下的storage.jsapi
<script src="dist/storage.js"></script>
复制代码
github地址数组