1.创建Storage.js文件
class Storage {
getStorage(key) {
const argLength = arguments.length
console.log(arguments)
if(argLength > 1) {
const getStorage = {}
const arg = Array.apply(null, arguments)
arg.forEach((element) => {
getStorage[element] = uni.getStorageSync(element);
})
return getStorage
} else {
return uni.getStorageSync(key);
}
}
setStorage(key, datas) {
const argLength = arguments.length
if(argLength > 1) {
const data = JSON.stringify(datas)
uni.setStorage({
key: key,
data: data,
success: function() {
console.log('success');
}
});
} else {
const arg = Array.apply(null, arguments)[0].entries()
arg.forEach((element) => {
uni.setStorage({
key: element[0],
data: element[1],
success: function() {
console.log('success');
}
});
})
}
}
removeStorage(key) {
const argLength = arguments.length
if (argLength > 1) {
const arg = Array.apply(null, arguments)
arg.forEach((element) => {
uni.removeStorage({
key: element
});
})
} else {
uni.removeStorage({
key: key
});
}
}
}
export default new Storage
2.使用
全局使用
import Vue from 'vue'
import Storage from 'util/Storage.js'
Vue.prototype.$Storage = Storage
const token = this.$Storage .getStorage('token')
局部使用
import Storage from 'util/Storage.js'
const token = Storage.getStorage('token')
剩余其他几个方法跟上述类似,若有错误和更好的提议请留言评论