//localStorage.js
const sessionStore = {
setlocalStorage(key, value){
if(typeof(value) == 'object'){
if(value == null){
//给出相应的提示就行
}else{
localStorage.setItem(key, JSON.stringify(value))
}
}else if(typeof(value) == 'function'){
localStorage.setItem(key, value.toString())
}else{
localStorage.setItem(key, value)
}
},
getlocalStorage(key, type){
if(type == 'fn'){
return eval('(' + localStorage.getItem(key) + ')')
}else{
return JSON.parse(localStorage.getItem(key))
}
}
}
const sessionStoreFn = (Vue) => {
Vue.$sessionStore = Vue.prototype.$sessionStore = sessionStore
}
export default {
install: sessionStoreFn
}
//main.js 全局引入注册
import sessionStore from "./utils/plugin"
Vue.use(sessionStore)
页面应用
this.$sessionStore.setlocalStorage('function', function(){console.log('0000')})
this.$sessionStore.getlocalStorage('function', 'fn')
注:这里如果想要存储函数 需要传第二个参数
这里根据需要可以将 localStorage 更改为sessionstorage