ue组件内嵌一个iframe,现在想要在iframe内获取父vue组件内信息,采用的是H5新特性PostMessage来解决跨域问题。

postMessage内涵两个API:

onMessage:消息监听

postMessage:消息发送

举个栗子,比如我要改变iframe内字体变成跟父级一样,直接上代码:

 <div class="mapbox">
      <iframe name="map" src="http://localhost:8083/setposition.html?add='add'"></iframe>
 </div>
clearMap(){
      let map = document.getElementsByName("map")[0].contentWindow
      map.postMessage("clearMap","*")
    }

iframe内:

window.addEventListener('message', function (evt) {
  
    if (evt.data == 'clearMap'){
        clearMap()
    }
    //event.data获取传过来的数据
});

 

相关文章:

  • 2021-09-14
  • 2022-12-23
  • 2021-08-18
  • 2022-12-23
  • 2021-08-31
  • 2021-10-24
  • 2021-12-12
猜你喜欢
  • 2021-06-19
  • 2022-12-23
  • 2022-12-23
  • 1970-01-01
  • 2022-01-10
  • 2021-05-18
相关资源
相似解决方案