父页面
获取dom,通过postMessage,发送参数
<iframe
name="test"
id="test"
width="100%"
height="100%"
src="http://127.0.0.1:5500/index.html"
></iframe>
onMounted(() => {
let iframe=document.querySelector("#test") as HTMLIFrameElement;
iframe.onload=() => {
console.log('iframe 已加载');
iframe.contentWindow.postMessage('hello world','*');
}
}),
子页面接收
监听message,进行接收
window.addEventListener('message', function(e){
console.log(e.data)
}