- main.js
Vue.prototype.$setStorageItem = function (key, newVal) {
//activeIndex监听的session字段
if (key === 'activeIndex') {
// 创建一个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);
}
}
- created或mounted
window.addEventListener('setItem', () => {
this.setActiveIndex(sessionStorage.getItem('activeIndex') || '3')
})
- 设置值
this.$setStorageItem('activeIndex', key)
网友评论