父级给iframe内部传递参数

方法1:很简单,直接在iframe的src数属性中拼接上参数

// 发送方:
<div>
    <iframe :src="url" id="childFrame" importance="high" name="demo" ></iframe>
</div>

url = `http://localhost:8080/couponManager?a={a}` //  

接收方:直接使用window.location.search接收,然后对接收到的进行处理

未完待续…

iframe内部给父级传递参数

方法1:使用postMessage方法跨域传递

// 发送方:
top.postMessage("hello", "http://localhost:8081"); // 要写top.postMessage或者是parent.message

// 接收方:
window.addEventListener( "message",
(e)=>{console.log(e.data)}	
false);

未完待续…

遇到的问题

1、问题:Failed to execute ‘postMessage’ on ‘DOMWindow’
如果遇到上面的问题可以看下这位大佬的解释:Failed to execute ‘postMessage’ on ‘DOMWindow’

上面的方法都是我自己亲自试过的,完全可用,如果小伙伴还有比较好的方法,可以留言给我,万分感谢~~

Logo

基于 Vue 的企业级 UI 组件库和中后台系统解决方案,为数万开发者服务。

更多推荐