国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

實(shí)現(xiàn)ifream內(nèi)外互相交互功能(vue2以及vue3寫法)

這篇具有很好參考價(jià)值的文章主要介紹了實(shí)現(xiàn)ifream內(nèi)外互相交互功能(vue2以及vue3寫法)。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問(wèn)。

1、首先,在創(chuàng)建一個(gè)iframe,指向被嵌套的頁(yè)面

vue3(src就是我們嵌套頁(yè)面的地址)(上面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、首先實(shí)現(xiàn)外層頁(yè)面調(diào)用iframe內(nèi)部頁(yè)面方法

a、在iframe內(nèi)部頁(yè)面監(jiān)聽message事件

    /**
     * @description  用于外層頁(yè)面調(diào)用ifream中的方法,在window掛載監(jiān)聽事件 如果接收到的關(guān)鍵字匹配調(diào)用相對(duì)應(yīng)的方法
     * @author: Destinynever
     * @param {String} message 監(jiān)聽message事件 此處監(jiān)聽的是通過(guò)window.postMessage API發(fā)送的消息
     * @param {Object} e 這是messageEvent對(duì)象 它包含有關(guān)觸發(fā)事件的詳細(xì)信息
     * @param {String} e.data 接收到的消息數(shù)據(jù),可以是字符串、對(duì)象或其他可以序列化的數(shù)據(jù)。
     * @param {String} e.origin 發(fā)送消息的窗口的來(lái)源,通常是一個(gè) URL。
     * @param {String} e.source 發(fā)送消息的窗口對(duì)象
     * @date 2023-12-27 09:56:00
     */
    window.addEventListener("message", (event) => {
      if (event.origin === "http://您外層頁(yè)面的地址:5173") {
        if (event.data === "testLog1") {
          this.testLog1();
        } else if (event.data === "testLog2") {
          this.testLog2();
        }
      }
    });

b、在iframe內(nèi)部監(jiān)聽定義兩個(gè)事件

    /**
     * @description 定義方法 提供給外層頁(yè)面調(diào)用
     * @author: Destinynever
     * @date 2023-12-27 10:23:00
     */
    testLog1() {
      console.log("成功調(diào)用iframe頁(yè)面中的testLog1方法");
    },

    testLog2() {
      console.log("成功調(diào)用iframe頁(yè)面中的testLog2方法");
    },

c、在外層頁(yè)面中發(fā)送消息調(diào)用iframe內(nèi)部事件(上面vue2 下面vue3)

    /**
     * @description 用于外層頁(yè)面調(diào)用iframe中頁(yè)面方法, 如果接收到的關(guān)鍵字匹配調(diào)用相對(duì)應(yīng)的方法
     * @author: Destinynever
     * @date 2023-12-27 09:56:00
     */
    iframeTestLog1Fn() {
      this.$refs.h5Ref.contentWindow.postMessage("testLog1", "http://內(nèi)層頁(yè)面ip:8089");
    },

    iframeTestLog2Fn() {
      this.$refs.h5Ref.contentWindow.postMessage("testLog2", "http://內(nèi)層頁(yè)面ip:8089");
    },
/**
 * @description 用于iframe中頁(yè)面調(diào)用外部方法,在window掛載監(jiān)聽事件 如果接收到的關(guān)鍵字匹配調(diào)用相對(duì)應(yīng)的方法
 * @author: Destinynever
 * @date 2023-12-27 09:56:00
 */
const h5Ref = ref();

const iframeTestLog1Fn = () => {
  if (h5Ref.value.contentWindow) {
    h5Ref.value.contentWindow.postMessage("testLog1", "http://內(nèi)部頁(yè)面ip:8089");
  }
};

const iframeTestLog2Fn = () => {
  if (h5Ref.value.contentWindow) {
    h5Ref.value.contentWindow.postMessage("testLog2", "http://內(nèi)部頁(yè)面ip:8089");
  }
};

3、實(shí)現(xiàn)在iframe頁(yè)面調(diào)用外層頁(yè)面中的方法

?a、在外層頁(yè)面監(jiān)聽message事件(上面vue2 下面vue3)

mounted() {
    let that = this;

    /**
     * @description 用于iframe中頁(yè)面調(diào)用外部方法,在window掛載監(jiān)聽事件 如果接收到的關(guān)鍵字匹配調(diào)用相對(duì)應(yīng)的方法
     * @author: Destinynever
     * @param {String} message 監(jiān)聽message事件 此處監(jiān)聽的是通過(guò)window.postMessage API發(fā)送的消息
     * @param {Object} e 這是messageEvent對(duì)象 它包含有關(guān)觸發(fā)事件的詳細(xì)信息
     * @param {String} e.data 接收到的消息數(shù)據(jù),可以是字符串、對(duì)象或其他可以序列化的數(shù)據(jù)。
     * @param {String} e.origin 發(fā)送消息的窗口的來(lái)源,通常是一個(gè) URL。
     * @param {String} e.source 發(fā)送消息的窗口對(duì)象
     * @date 2023-12-27 09:56:00
     */
    window.addEventListener("message", (e) => {
      if (e.origin === "http://內(nèi)層頁(yè)面ip:8089") {
        if (e.data === "fatherFn1") {
          that.fatherFn1();
        }
        if (e.data === "fatherFn2") {
          that.fatherFn2();
        }
      }
    });
  },
onMounted(() => {
  /**
   * @description 用于iframe中頁(yè)面調(diào)用外部方法,在window掛載監(jiān)聽事件 如果接收到的關(guān)鍵字匹配調(diào)用相對(duì)應(yīng)的方法
   * @author: Destinynever
   * @param {String} message 監(jiān)聽message事件 此處監(jiān)聽的是通過(guò)window.postMessage API發(fā)送的消息
   * @param {Object} e 這是messageEvent對(duì)象 它包含有關(guān)觸發(fā)事件的詳細(xì)信息
   * @param {String} e.data 接收到的消息數(shù)據(jù),可以是字符串、對(duì)象或其他可以序列化的數(shù)據(jù)。
   * @param {String} e.origin 發(fā)送消息的窗口的來(lái)源,通常是一個(gè) URL。
   * @param {String} e.source 發(fā)送消息的窗口對(duì)象
   * @date 2023-12-27 09:56:00
   */
  window.addEventListener("message", (e) => {
    if (e.origin === "http://內(nèi)層頁(yè)面ip:8089") {
      if (e.data === "fatherFn1") {
        fatherFn1();
      }
      if (e.data === "fatherFn2") {
        fatherFn2();
      }
    }
  });
});

b、在外層頁(yè)面監(jiān)聽定義兩個(gè)事件(上面vue2 下面vue3)


    /**
     * @description 在外層頁(yè)面定義方法 提供給ifream頁(yè)面調(diào)用
     * @author: Destinynever
     * @date 2023-12-27 09:56:00
     */
    fatherFn1() {
      console.log("成功調(diào)用了父組件中的fatherFn1方法");
    },
    fatherFn2() {
      console.log("成功調(diào)用了父組件中的fatherFn2方法");
    },
/**
 * @description 在外層頁(yè)面定義方法 提供給ifream頁(yè)面調(diào)用
 * @author: Destinynever
 * @date 2023-12-27 09:56:00
 */
const fatherFn1 = () => {
  console.log("成功調(diào)用了父組件中的fatherFn1方法");
};
const fatherFn2 = () => {
  console.log("成功調(diào)用了父組件中的fatherFn2方法");
};

c、在iframe內(nèi)部頁(yè)面中發(fā)送消息調(diào)用外層頁(yè)面事件文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-854490.html

    /**
     * @description 通過(guò)window的postMessage向外層發(fā)送消息以調(diào)用外層方法
     * @author: Destinynever
     * @date 2023-12-27 10:23:00
     */
    testFatherFn1() {
      window.parent.postMessage("fatherFn1", "http://外層頁(yè)面ip:5173");
    },
    testFatherFn2() {
      window.parent.postMessage("fatherFn2", "http://外層頁(yè)面ip:5173");
    },

到了這里,關(guān)于實(shí)現(xiàn)ifream內(nèi)外互相交互功能(vue2以及vue3寫法)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來(lái)自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

覺(jué)得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包