vue使用window.addEventListener()

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
喜欢就支持一下吧
点赞0 分享