javascript - react-native-webview this.webView.postMessage 不起作用
问题描述
以前的版本不工作,因为它是新的。
"react-native-webview": "^9.0.2" "react-native": "0.62.0",
2. webview options
javaScriptEnabled={true}
mediaPlaybackRequiresUserAction={false}
startInLoadingState={true}
allowUniversalAccessFromFileURLs={true}
onMessage={this.respondToOnMessage}
3. react-native code
this.webView.postMessage( "Post message from react native" );
4. web code
function receiveMessage(e) {
callbackFunction(JSON.parse(e.data));
}
function RNPostMessage(data) {
window.ReactNativeWebView.postMessage(data);
}
window.addEventListener('message', receiveMessage, true);
5. react-native error msg
D:\dev\projects\mobile-app\react-native\yc_attend\node_modules\react-devtools-core\dist\backend.js:32 Possible Unhandled Promise Rejection (id: 0):
TypeError: Cannot read property 'postMessage' of undefined
TypeError: Cannot read property 'postMessage' of undefined
我想知道这个问题的解决方案...
解决方案
你必须尝试做injectJavascript
而不是this.webview.postMessage()
。我以前也这样做过,并且可以在 webview 中获取我的数据。尝试在 react-native 的注入 Javascript 中使用 window.postMessage。react-native-webview 中有用于注入 javascript 的道具。
推荐阅读
- java - 在 Redis 中存储 Web Socket 会话
- angular - Angular 无法分配给只读属性
- javascript - 如何将外部 js 文件集成到 Angular 6 中。特别是将 Angular 质粒集成到 Angular 6 中
- php - 无法通过 XAMMP 使用 PHP 连接到 Oracle Database 11g
- php - 使用 CURL 向 SSL 认证域发送 post 请求
- php - 无法在 Excel::load() 之外获取对象名称值
- ajax - 如何更改日期格式 Ajax codeigniter 记录列表..?
- java - 如何使用 JAVA 从 XML 中获取特定的整个标签值
- python - 如何在 GPU 的支持下运行 python 代码
- ios - Swift 4 - 使用 URLSession 获取上传图像进度