- 在main.js中添加全局事件监控方法
 
Vue.prototype.resetSetItem = function (key, newVal) {
  if (key === 'plan') {
    // 创建一个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('plan',JSON.stringify(res.data));
- 在created方法里面监听
 
window.addEventListener('setItem', ()=> {
    this.msg = sessionStorage.getItem('plan');
});