setItem(name, value, expires) {标签:储存,vue,return,name,expires,console,localStorage,obj From: https://www.cnblogs.com/javalinux/p/16908094.html
if (!localStorage) {
console.log('unsupport localStorage');
return;
}
var obj = {};
obj.value = value;
if (expires) obj.expires = Date.now() + expires;
localStorage.setItem(name, JSON.stringify(obj));
},
getItem(name) {
if (!localStorage) {
console.log('unsupport localStorage');
return;
}
var objStr = localStorage.getItem(name);
var obj;
try {
obj = JSON.parse(objStr);
} catch (e) {
console.log('json parse error:');
console.log(e);
}
if (!obj) return;
if (obj.expires && obj.expires < Date.now()) {
localStorage.removeItem(name);
return;
}
return obj.value;
}