小编les*_*ode的帖子

如何从 react 中获取 postmessage 数据?

我正在尝试将 react url 作为 iframe 加载到我的 jsp 项目中。

这是我的发送方代码块:

<iframe id="eda" 
               style="display: none;"
                src="http://myhost:3000/"
                width="100%" height="600" border="0" marginwidth="0"
                marginheight="0" scrolling="no">
</iframe>   

****

function loadReactIframe(){
    document.getElementById("eda").contentWindow.postMessage('GET MESSAGE FROM ME', '*');
}
Run Code Online (Sandbox Code Playgroud)

我还尝试了以下方法:

function loadReactIframe(){
      document.getElementById("eda").contentWindow.postMessage(
            'GET MESSAGE FROM ME', 
            'http://myhost', 3000
     );
}
Run Code Online (Sandbox Code Playgroud)

我的接收器(反应)代码块:

componentDidMount() {
     window.addEventListener('load', this.handleLoad);
     alert('componentDidMount')
}

handleLoad(event) {
     alert(event.data);
}
Run Code Online (Sandbox Code Playgroud)

但我无法从事件中获取数据。

javascript postmessage reactjs

8
推荐指数
1
解决办法
9369
查看次数

标签 统计

javascript ×1

postmessage ×1

reactjs ×1