iframe向父级传递参数(用postMessage来解决跨域)

父页面

<iframe src="http://localhost:8080/index.html" id="vrId"></iframe>

父页面接收子页面的参数

mounted(){
  window.addEventListener('message', function(e){
        console.log(e.data)
    })  
}

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 iframe页面

created(){
  top.postMessage(需要传递给父页面的参数)  
}

注意:如果使用window.postMessage()  可能会出现   Failed to execute 'postMessage' on 'DOMWindow'     报错
其实 top就是指向iframe最顶层的窗口,建议是用  top.postMessage()即可

posted @ 2021-05-28 17:52  龙卷风吹毁停车场  阅读(714)  评论(0编辑  收藏  举报