1.监听路由改变
watch: {
$route() {
this.getLabels()
},
},
2.监听多个页面的数据变化如列表->详情
Vue.prototype.addStorageEvent = function (type, key, data) {
if (type === 1) {
const newStorageEvent = document.createEvent('StorageEvent')
const storage = {
setItem: function (k, val) {
localStorage.setItem(k, val)
newStorageEvent.initStorageEvent(
'setItem',
false,
false,
k,
null,
val,
null,
null
)
window.dispatchEvent(newStorageEvent)
},
}
return storage.setItem(key, data)
} else {
const 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, data)
}
}
window.addEventListener('storage', (e) => {
if (e.newValue == this.caseDetail.id) {
this.caseDetail.mark = !this.caseDetail.mark
}
localStorage.removeItem('caseDetail_mark')
})
this.addStorageEvent(1, 'caseDetail_mark', row.id)
created() {
window.addEventListener('storage', (e) => {
const item = this.list.find((item) => item.evidence_id == e.newValue)
if (item != undefined) {
item.mark = !item.mark
}
localStorage.removeItem('caseDetail_mark')
})
this.addStorageEvent(1, 'caseDetail_mark', row.evidence_id)