使用iframe的起因:
现存在两个不同的系统,A系统和B系统。因为某中原因,A系统要调用B系统页面中的个别页面,所以引入了iframe;并且两个系统之间需要通信,设计到跨域问题,就有了postmessage;
在系统中的代码示例:
<div style="height:500px;">
<iframe style="width:100%;height:100%;" src={this.iframeUrl} ref="xxxIframe" title="xxx" name="xxx" id="xxx" />
</div>
loadIform(): void {
const self: any = this;
const iFrame = self.$refs.xxiframe;
if (!iFrame) {
return;
}
iFrame.onload = (): void => {
setTimeout(() => {
iFrame.contentWindow.postMessage(数据, '*');
}, 600);
};
window.onmessage = (event) => {
console.log(event.data, '@@@@@@');
};
}
注意点:
因为我的是一个弹出框,然后嵌入的iframe,所以添加了延迟了。
注意判断不同的源。
postmessage详解地址