- 在main.js中添加全局事件監(jiān)控方法
Vue.prototype.resetSetItem = function (key, newVal) {
if (key === 'plan') {
// 創(chuàng)建一個(gè)StorageEvent事件
var newStorageEvent = document.createEvent('StorageEvent');
const storage = {
setItem: function (k, val) {
sessionStorage.setItem(k, val);
// 初始化創(chuàng)建的事件
newStorageEvent.initStorageEvent('setItem', false, false, k, null, val, null, null);
// 派發(fā)對(duì)象
window.dispatchEvent(newStorageEvent)
}
}
return storage.setItem(key, newVal);
}
}
- 得到數(shù)據(jù)
this.resetSetItem('plan',JSON.stringify(res.data));
- 在created方法里面監(jiān)聽(tīng)
window.addEventListener('setItem', ()=> {
this.msg = sessionStorage.getItem('plan');
});