当前位置:   article > 正文

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

ifream

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

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

  1. <template>
  2. <div>
  3. <div @click="iframeTestLog1Fn">iframeTestLog1Fn</div>
  4. <div @click="iframeTestLog2Fn">iframeTestLog2Fn</div>
  5. <iframe style="height: 700px; width: 400px" ref="h5Ref" id="h5Ref" src="http://ip地址:8089"></iframe>
  6. </div>
  7. </template>
  1. <template>
  2. <div @click="iframeTestLog1Fn">iframeTestLog1Fn</div>
  3. <div @click="iframeTestLog2Fn">iframeTestLog2Fn</div>
  4. <iframe style="height: 700px; width: 400px" ref="h5Ref" id="h5Ref" src="http://ip地址:8089"></iframe>
  5. </template>

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

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

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

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

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

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

  1. /**
  2. * @description 用于外层页面调用iframe中页面方法, 如果接收到的关键字匹配调用相对应的方法
  3. * @author: Destinynever
  4. * @date 2023-12-27 09:56:00
  5. */
  6. iframeTestLog1Fn() {
  7. this.$refs.h5Ref.contentWindow.postMessage("testLog1", "http://内层页面ip:8089");
  8. },
  9. iframeTestLog2Fn() {
  10. this.$refs.h5Ref.contentWindow.postMessage("testLog2", "http://内层页面ip:8089");
  11. },
  1. /**
  2. * @description 用于iframe中页面调用外部方法,在window挂载监听事件 如果接收到的关键字匹配调用相对应的方法
  3. * @author: Destinynever
  4. * @date 2023-12-27 09:56:00
  5. */
  6. const h5Ref = ref();
  7. const iframeTestLog1Fn = () => {
  8. if (h5Ref.value.contentWindow) {
  9. h5Ref.value.contentWindow.postMessage("testLog1", "http://内部页面ip:8089");
  10. }
  11. };
  12. const iframeTestLog2Fn = () => {
  13. if (h5Ref.value.contentWindow) {
  14. h5Ref.value.contentWindow.postMessage("testLog2", "http://内部页面ip:8089");
  15. }
  16. };

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

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

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

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

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

c、在iframe内部页面中发送消息调用外层页面事件

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

本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号