1、创建 storage.js 封装下方法
export const setSessionStorage = (k, v) => { // 设置
if (!k || !v) return
var json = JSON.stringify(v)
sessionStorage.setItem(k, json)
}
export const getSessionStorage = k => { // 获取
if (!k) return
let data = null
let datajson = sessionStorage.getItem(k)
if (datajson) {
datajson = JSON.parse(datajson)
}
data = datajson
return data
}
export const removeSessionStorage = k => { // 删除
if (!k) return
sessionStorage.removeItem(k)
}
2、页面调用
import { setSessionStorage, getSessionStorage , removeSessionStorage } from "./storage"
setSessionStorage('setName',val) // 设置
const getName = getSessionStorage('setName') // 获取
destroyed () {
removeSessionStorage('setName',val) // 删除销毁
}
storage.js
// localStorage
export const setStorage = (k, v) => {
if (!k || !v) return
var json = JSON.stringify(v)
window.localStorage.setItem(k, json)
}
export const getStorage = k => {
if (!k) return
let data = null
let datajson = window.localStorage.getItem(k)
datajson = JSON.parse(datajson)
data = datajson
return data
}
export const removeStorage = k => {
if (!k) return
window.localStorage.removeItem(k)
}
// sessionStorage
export const setSessionStorage = (k, v) => {
if (!k || !v) return
var json = JSON.stringify(v)
sessionStorage.setItem(k, json)
}
export const getSessionStorage = k => {
if (!k) return
let data = null
let datajson = sessionStorage.getItem(k)
datajson = JSON.parse(datajson)
data = datajson
return data
}
export const removeSessionStorage = k => {
if (!k) return
sessionStorage.removeItem(k)
}
// 组件引用
// import {
// setSessionStorage,
// getSessionStorage,
// removeSessionStorage
// } from '@/utils/storage'
// setSessionStorage('serveMarketDetail', data)
// const target = JSON.parse(getSessionStorage('serveMarketDetail'))
// removeSessionStorage('serveMarketDetail')