实现ifream内外互相交互功能(vue2以及vue3写法)

这篇具有很好参考价值的文章主要介绍了实现ifream内外互相交互功能(vue2以及vue3写法)。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

1、首先,在创建一个iframe,指向被嵌套的页面

vue3(src就是我们嵌套页面的地址)(上面vue2 下面vue3)

<template>
  <div>
    <div @click="iframeTestLog1Fn">iframeTestLog1Fn</div>
    <div @click="iframeTestLog2Fn">iframeTestLog2Fn</div>
    
    <iframe style="height: 700px; width: 400px" ref="h5Ref" id="h5Ref" src="http://ip地址:8089"></iframe>
  </div>
</template>
<template>
  <div @click="iframeTestLog1Fn">iframeTestLog1Fn</div>
  <div @click="iframeTestLog2Fn">iframeTestLog2Fn</div>

  <iframe style="height: 700px; width: 400px" ref="h5Ref" id="h5Ref" src="http://ip地址:8089"></iframe>
</template>

2、首先实现外层页面调用iframe内部页面方法

a、在iframe内部页面监听message事件

    /**
     * @description  用于外层页面调用ifream中的方法,在window挂载监听事件 如果接收到的关键字匹配调用相对应的方法
     * @author: Destinynever
     * @param {String} message 监听message事件 此处监听的是通过window.postMessage API发送的消息
     * @param {Object} e 这是messageEvent对象 它包含有关触发事件的详细信息
     * @param {String} e.data 接收到的消息数据,可以是字符串、对象或其他可以序列化的数据。
     * @param {String} e.origin 发送消息的窗口的来源,通常是一个 URL。
     * @param {String} e.source 发送消息的窗口对象
     * @date 2023-12-27 09:56:00
     */
    window.addEventListener("message", (event) => {
      if (event.origin === "http://您外层页面的地址:5173") {
        if (event.data === "testLog1") {
          this.testLog1();
        } else if (event.data === "testLog2") {
          this.testLog2();
        }
      }
    });

b、在iframe内部监听定义两个事件

    /**
     * @description 定义方法 提供给外层页面调用
     * @author: Destinynever
     * @date 2023-12-27 10:23:00
     */
    testLog1() {
      console.log("成功调用iframe页面中的testLog1方法");
    },

    testLog2() {
      console.log("成功调用iframe页面中的testLog2方法");
    },

c、在外层页面中发送消息调用iframe内部事件(上面vue2 下面vue3)

    /**
     * @description 用于外层页面调用iframe中页面方法, 如果接收到的关键字匹配调用相对应的方法
     * @author: Destinynever
     * @date 2023-12-27 09:56:00
     */
    iframeTestLog1Fn() {
      this.$refs.h5Ref.contentWindow.postMessage("testLog1", "http://内层页面ip:8089");
    },

    iframeTestLog2Fn() {
      this.$refs.h5Ref.contentWindow.postMessage("testLog2", "http://内层页面ip:8089");
    },
/**
 * @description 用于iframe中页面调用外部方法,在window挂载监听事件 如果接收到的关键字匹配调用相对应的方法
 * @author: Destinynever
 * @date 2023-12-27 09:56:00
 */
const h5Ref = ref();

const iframeTestLog1Fn = () => {
  if (h5Ref.value.contentWindow) {
    h5Ref.value.contentWindow.postMessage("testLog1", "http://内部页面ip:8089");
  }
};

const iframeTestLog2Fn = () => {
  if (h5Ref.value.contentWindow) {
    h5Ref.value.contentWindow.postMessage("testLog2", "http://内部页面ip:8089");
  }
};

3、实现在iframe页面调用外层页面中的方法

 a、在外层页面监听message事件(上面vue2 下面vue3)

mounted() {
    let that = this;

    /**
     * @description 用于iframe中页面调用外部方法,在window挂载监听事件 如果接收到的关键字匹配调用相对应的方法
     * @author: Destinynever
     * @param {String} message 监听message事件 此处监听的是通过window.postMessage API发送的消息
     * @param {Object} e 这是messageEvent对象 它包含有关触发事件的详细信息
     * @param {String} e.data 接收到的消息数据,可以是字符串、对象或其他可以序列化的数据。
     * @param {String} e.origin 发送消息的窗口的来源,通常是一个 URL。
     * @param {String} e.source 发送消息的窗口对象
     * @date 2023-12-27 09:56:00
     */
    window.addEventListener("message", (e) => {
      if (e.origin === "http://内层页面ip:8089") {
        if (e.data === "fatherFn1") {
          that.fatherFn1();
        }
        if (e.data === "fatherFn2") {
          that.fatherFn2();
        }
      }
    });
  },
onMounted(() => {
  /**
   * @description 用于iframe中页面调用外部方法,在window挂载监听事件 如果接收到的关键字匹配调用相对应的方法
   * @author: Destinynever
   * @param {String} message 监听message事件 此处监听的是通过window.postMessage API发送的消息
   * @param {Object} e 这是messageEvent对象 它包含有关触发事件的详细信息
   * @param {String} e.data 接收到的消息数据,可以是字符串、对象或其他可以序列化的数据。
   * @param {String} e.origin 发送消息的窗口的来源,通常是一个 URL。
   * @param {String} e.source 发送消息的窗口对象
   * @date 2023-12-27 09:56:00
   */
  window.addEventListener("message", (e) => {
    if (e.origin === "http://内层页面ip:8089") {
      if (e.data === "fatherFn1") {
        fatherFn1();
      }
      if (e.data === "fatherFn2") {
        fatherFn2();
      }
    }
  });
});

b、在外层页面监听定义两个事件(上面vue2 下面vue3)


    /**
     * @description 在外层页面定义方法 提供给ifream页面调用
     * @author: Destinynever
     * @date 2023-12-27 09:56:00
     */
    fatherFn1() {
      console.log("成功调用了父组件中的fatherFn1方法");
    },
    fatherFn2() {
      console.log("成功调用了父组件中的fatherFn2方法");
    },
/**
 * @description 在外层页面定义方法 提供给ifream页面调用
 * @author: Destinynever
 * @date 2023-12-27 09:56:00
 */
const fatherFn1 = () => {
  console.log("成功调用了父组件中的fatherFn1方法");
};
const fatherFn2 = () => {
  console.log("成功调用了父组件中的fatherFn2方法");
};

c、在iframe内部页面中发送消息调用外层页面事件文章来源地址https://www.toymoban.com/news/detail-854490.html

    /**
     * @description 通过window的postMessage向外层发送消息以调用外层方法
     * @author: Destinynever
     * @date 2023-12-27 10:23:00
     */
    testFatherFn1() {
      window.parent.postMessage("fatherFn1", "http://外层页面ip:5173");
    },
    testFatherFn2() {
      window.parent.postMessage("fatherFn2", "http://外层页面ip:5173");
    },

到了这里,关于实现ifream内外互相交互功能(vue2以及vue3写法)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • vue2+antd——实现动态菜单路由功能——基础积累

    最近在写后台管理系统,遇到一个需求就是要将之前的静态路由改为动态路由,使用的后台框架是: vue-antd-admin 然后通过 loadRoutes 方法来实现异步动态路由。 如上图所示,需要在登录接口调用成功后,书写以下的代码: import { loadRoutes } from \\\'@/utils/routerUtil.js\\\'; import { getCodeL

    2024年02月08日
    浏览(29)
  • Android 原生功能与 Vue 交互实现

    前端用 Android webview 嵌入 vue 地址,如何在vue 页面中显示 Android 版本号 从Android中将该信息传递给Vue应用程序。可以通过使用WebView的Java Bridge来实现此目的。这里是一些可能有用的步骤: 在你的Android代码中,使用getBuildVersionName()或getBuildVersionCode()等方法来获取Android的版本号。

    2024年04月24日
    浏览(25)
  • SpringBoot和Vue2集成WebSocket,实现聊天室功能

    springboot集成websocket实现聊天室的功能。如有不足之处,还望大家斧正。

    2024年01月23日
    浏览(36)
  • vue2 - 基于Element UI实现上传Excel表单数据功能

    批量数据上传后台,需要从后台下载一个固定格式的 Excel表格,然后在表格里面添加数据,将数据格式化,再上传给后台,后台做解析处理,往数据库添加数据 点击导入excel按钮,跳转到上传excel功能页面,点击上传或者是通过拖拽都能实现excel表格上传 通过Element UI的 el-di

    2024年02月13日
    浏览(30)
  • vue2实现二进制流pdf浏览器预览功能

    该方法不需要使用插件  获取后端二进制文件流后直接处理 然后点击调用方法使用

    2024年01月20日
    浏览(50)
  • Python使用Matplotlib通过鼠标交互实现缩放、移动以及线上点坐标显示功能

    参考文章: 缩放:python 桌面软件开发-matplotlib画图鼠标缩放拖动_matplotlib缩放-CSDN博客 获取点坐标参考的文章忘了,侵权即删

    2024年04月13日
    浏览(30)
  • Vue2 集成 CodeMirror 实现公式编辑、块状文本编辑,TAG标签功能

    效果图  安装codemirror依赖 本示例为Vue2项目,安装低版本的依赖 实现 实现代码如下,里边涉及到的变量和函数自行替换即可,没有其他复杂逻辑。

    2024年02月10日
    浏览(28)
  • 【h5+微信小程序】vue2实现h5扫码登录功能

    需要实现在同域名的h5页面上增加一个微信扫码登录的功能,如果用户已经有小程序的账号,可以直接登录。 使用 :vue2+微信小程序原生开发 可以实现上述功能的 前提 是:同一用户,对同一个微信开放平台下的不同应用,UnionID是相同的。域名已经配置。 可以用什么来区分

    2024年02月14日
    浏览(48)
  • vue2移动端使用vue-qrcode-reader实现扫一扫功能

    移动端实现扫一扫   扫码功能 第一种:如果是用uniapp开发  可以直接使用uni的语法 并且兼容多端 第二种:如果是开发浏览器的网页,基于微信的话,也可以用微信的weixin-js-sdk         具体流程参考官网:概述 | 微信开放文档 第三种:用第三方vue-qrcode-reader实现扫一扫功能

    2024年02月07日
    浏览(26)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包