1.js
export const monitorLocalStorage = () => {
const signSetItem = localStorage.setItem
localStorage.setItem = function (key, val) {
let setEvent = new Event('setItemEvent')
setEvent.key = key
setEvent.newValue = val
window.dispatchEvent(setEvent)
signSetItem.apply(this, arguments)
}
}
2.main.js全局引入
import {monitorLocalStorage } from './util/tools'
Vue.use(dispatchEventStroage) //监听颜色本地存储变化
3.组件使用
created() {
window.addEventListener("setItemEvent", (e)=> {
const newdata = JSON.parse(e.newValue);
console.log(newdata);
});
},