Bootstrap

vue中对localStorage的监听(谷歌+火狐)

1、utils文件下创建listeningWindow.js文件,并写如下方法
function dispatchEventStroage() {
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);
};
}
export default dispatchEventStroage;
2、在main.js中引入
import listeningWindow from “./utils/listeningWindow”;
Vue.use(listeningWindow);
3、在需要的模块中存值并监听
方法一:只在谷歌中进行Windows监听
methods: {
handlerowChange(row) {
localStorage.setItem(“TOPtabdetail”, JSON.stringify(row));
},
},
mounted() {
let self = this;
window.addEventListener(“setItemEvent”, function listenMsg(e) {
if (e.key === “TO

;