使用JSON.parse()和JSON.stringify()封装localstorage
const storage = {}
storage.get = function(name) {
return JSON.parse(localStorage.getItem(name))
}
storage.set = (name, val) => { //ES6写法
localStorage.setItem(name, JSON.stringify(val))
}
storage.add = function(name, addVal) {
const oldVal = storage.get(name)
const newVal = oldVal.concat(addVal)
storage.set(name, newVal)
}
storage.remove = function(name) {
localStorage.removeItem(name)
}
//向外暴露storage
export default storage
使用
uploadList: storage.get('upload') || [],