解决vue使用sessionStorage存储数据,页面不能实时更新

export const getSessionStorage = function (key, newVal) {
// 创建一个StorageEvent事件
const newStorageEvent = document.createEvent(‘StorageEvent’)
const storage = {
setItem: function (k, val) {
sessionStorage.setItem(k, val)
// 初始化创建的事件
newStorageEvent.initStorageEvent(‘setItem’, false, false, k, null, val, null, null)
// 派发对象
window.dispatchEvent(newStorageEvent)
}
}
return storage.setItem(key, newVal)
}
引入后直接调用
getSessionStorage(key, newVal)
参考https://blog.csdn.net/weixin_43896125/article/details/109996394

你可能感兴趣的:(总结,js,javascript,vue.js)