vue中iframe的 postMessage message的使用,以及一些小疑问

18 篇文章 0 订阅
11 篇文章 0 订阅
一、father页面
template:
<iframe ref="iframe" src="http://192.168.31.53:8080/" frameborder="1" id="iframe" width="400px" height="400px"></iframe>

script:
mounted() {
  // // 1, 加载完成自动发送消息
  // // 向iframe发送消息
  // if (this.$refs['iframe']) {
  //   // iframe加载完成
  //   this.$refs['iframe'].onload = e => {
  //     this.$refs['iframe'].contentWindow.postMessage('father的信息', '*');
  //   };
  // }

  // 监听消息
  window.addEventListener('message', fn, false);
  function fn(e) {
    if (e.origin == 'http://192.168.31.53:8080' && typeof e.data == 'string') {
      console.log('-------- iframe穿回来的数据 --------', e.data);
      // iframe.remove(); // 说明iframe已经接受到数据咯, 所以根据情况移除
      localStorage.setItem('test', e.data);
    }
  }
},

methods: {
  // 2、手动发送消息
  handleClickSend() {
    if (this.$refs['iframe']) {
      console.log('-------- 加载成功 --------');
      this.$refs['iframe'].contentWindow.postMessage('father的信息', 'http://192.168.31.53:8080');
    }
  }
}

2、iframe页面
script:
mounted() {
  window.addEventListener('message', fn, false);
  function fn(e) {
    if (e.origin == 'http://192.168.31.53:8081' && typeof e.data == 'string') {
      console.log('-------- 父级穿下来的信息 --------', e.data);
      localStorage.setItem('test', e.data);
      // 发布消息给父级, 父级同样的订阅 message 接受
      window.parent.postMessage('ready', 'http://192.168.31.53:8081');
    }
  }
},

三、小疑问以及自己的理解,如图

小疑问?点击按钮之后才发送的信息 , 可以看到不管是在父级打印的还是在iframe页面打印的数据在父级页面都能看到, 但是在iframe中的却没有看到的问题,是不是说明在父级发送消息以后iframe通过message事件没有执行呢?

答案:iframe是执行了的并且拿到数据了的

如下图,订阅消息之后我都存在本地的,本地都存在,说明了iframe的message是执行了的

四、为什么要存本地(题外话)

        是因为单点登录(跨域的情况),就可以利用iframe postMeaage于message进行通讯,将token存入其他网站的本地存储中,实现单点登录

Vue中使用iframe 、加载完成后回调事件_vue iframe onload_snows_l的博客-CSDN博客https://blog.csdn.net/snows_l/article/details/127921558?spm=1001.2014.3001.5501

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
Vue3使用iframepostMessage功能,可以通过以下步骤实现: 1. 首先,在Vue组件获取到iframe的DOM元素。可以使用`document.querySelector`或者Vue的`ref`属性来获取。例如,使用`ref`属性获取到iframe元素的示例代码如下: ```javascript <template> <iframe ref="myIframe" src="http://example.com"></iframe> </template> <script> export default { mounted() { const iframe = this.$refs.myIframe; // 在这里可以对iframe进行操作 } } </script> ``` 2. 接下来,监听iframe的`onload`事件,确保iframe已经加载完成。在事件处理程序,可以使用`contentWindow.postMessage`方法向iframe发送消息。示例代码如下: ```javascript mounted() { const iframe = this.$refs.myIframe; iframe.onload = () => { console.log('iframe已加载'); iframe.contentWindow.postMessage('hello world', '*'); } } ``` 3. 在子页面,可以使用`window.addEventListener`方法监听消息事件。通过判断消息的来源和数据类型,可以对接收到的消息进行处理。示例代码如下: ```javascript mounted() { window.addEventListener('message', (e) => { if (e.origin === 'http://example.com' && typeof e.data === 'string') { console.log('接收到来自iframe的数据', e.data); // 根据情况进行处理 } }, false); } ``` 这样,你就可以在Vue3使用iframepostMessage功能了。通过监听事件和发送消息,可以实现Vue组件iframe之间的通信。注意,在实际使用,需要根据实际情况设置正确的来源和目标URL。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [vue3 iframe postmessage使用](https://blog.csdn.net/qq_46019581/article/details/127464930)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [vueiframepostMessage message使用,以及一些小疑问](https://blog.csdn.net/snows_l/article/details/131532067)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

snows_l

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值