方法封装:
updateStorage(name, value){ sessionStorage.setItem('name, newVal); const storageEvent = new Event('storage'); Object.defineProperty(storageEvent, 'key', { value: name, enumerable: true }) Object.defineProperty(storageEvent, 'newValue', { value: newVal, enumerable: true }) window.dispatchEvent(storageEvent) }
使用:index.vue
mounted() { window.addEventListener('storage', this.handleStorageEvent); }, beforeDestroy() { window.removeEventListener('storage', this.handleStorageEvent); },methods(){ handleStorageEvent(event){ if(event.key === 'latitude'){ let data = event.newValue; } } }
标签:name,storage,value,监听,window,storageEvent,数据,event From: https://www.cnblogs.com/chenwan1218/p/17588555.html