removeEventListener与addEventListener要调用相同方法
mounted() {
//接收来自其他页面的消息并判断发送者
window.addEventListener("message", this.onMessage);
},
methods: {
onMessage(e) {
//判断消息来源
if (e.origin !== projectInfo.otherWindow.host && e.origin !== this.host) {
return;
}
let data = e.data;
// 判断消息id
if (data.msgId !== projectInfo.otherWindow.massageId) {
return;
}
//判断数据类型
if (data.type == projectInfo.otherWindow.massageType.exit) {
//退出关闭窗口
window.close();
return;
}
},
},
destroyed() {
// 在组件生命周期结束的时候销毁。
window.removeEventListener("message", this.onMessage);
},
复制代码
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END