vue监听缓存信息
在main.js中原型链上导入函数
Vue.prototype.resetSetItem = function(key, newVal) {
if (key === 'watchStorage') {
// 创建一个StorageEvent事件
var 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);
}
}
在需要创建缓存处添加
this.resetSetItem('watchStorage', JSON.stringify(obj));
在需要监听页面初添加
let that=this;
window.addEventListener('setItem', ()=> {
that.promodels=JSON.parse( sessionStorage.getItem('watchStorage'));
})