uni-app app使用web-view嵌入h5 无法向h5通信 一直报错

uni-app app使用web-view嵌入h5 无法向h5通信 一直报错

开发环境 版本号 项目创建方式
Mac 15.2 HBuilderX

产品分类:uniapp/App

PC开发环境操作系统:Mac

PC开发环境操作系统版本号:15.2

HBuilderX类型:正式

HBuilderX版本号:4.76

手机系统:iOS

手机系统版本号:iOS 18

手机厂商:苹果

手机机型:iphone13

页面类型:vue

vue版本:vue3

打包方式:云端

项目创建方式:HBuilderX


示例代码:

app端:

<template>
  <!-- <button [@click](/user/click)="sendMessageToWeb"></button> -->
  <view>
    <web-view ref="webView"  @message="onMessageFromWeb" @error="handleWebViewError" src="http://172.16.9.163:3001/work/index?transition=1"></web-view>
  </view>
</template> 
<script setup>
import { reactive, toRefs, onBeforeMount, onMounted, ref } from 'vue'
const webView = ref(null)
const state = reactive({ })

// 接收网页发送过来的消息
const onMessageFromWeb = (e) => {
console.log(e.detail.data[0], "onMessageFromWeb")
const type = e.detail.data[0].action
sendMessageToWeb()
}

// 处理 WebView 加载错误(如 H5 地址不可访问)
const handleWebViewError = (e)=> {
console.error('WebView 加载错误', e);
uni.showToast({ title: '页面加载失败,请重试', icon: 'none' });
}

// 向 WebView 发送消息
const sendMessageToWeb = () => {
if (!webView.value) return;
// 调用 H5 全局函数(H5 需提前定义 window.handleAppReply)
const replyScript = ` if (window.handleAppMessage) { window.handleAppMessage(${JSON.stringify({ a: 10 })}); } `;
// 通过 evalJS 执行 H5 函数,传递回复数据
webView.value.evalJS(replyScript);
}
onBeforeMount(() => {})
onMounted(() => {})
</script>
<style lang='scss' scoped></style>

h5端:
```vue
<!-- 工作台 -->
<template>
<div style="padding-top: 200px;">
工作台
<button [@click](/user/click)="handleSend">发送消息</button>
<Tabbar :activeTab="1"></Tabbar>
</div>
</template> 

<script setup>
defineOptions({
name: 'workIndex'
})
import { reactive, toRefs, onBeforeMount, onMounted } from 'vue'
const state = reactive({ })

const handleSend = () => {
webViewJs.postMessage({ data: { action: 'xxx' } })
}

onBeforeMount(() => {})
onMounted(() => { 
window.handleAppMessage = (data) => {  
    console.log('收到 App 回复', data);  
    alert('收到 App 回复' + JSON.stringify(data));  
}; 
})
</script>
<style lang='scss' scoped></style>

更多关于uni-app app使用web-view嵌入h5 无法向h5通信 一直报错的实战教程也可以访问 https://www.itying.com/category-93-b0.html

2 回复

提下更多线索,测试下 HBuilderX 内置的 hellouniapp 是否正常,你测试其他机器是否正常。打印 webview.value 是有值还是 null 没赋值?提供更多信息,有助于定位和解答你的问题。

更多关于uni-app app使用web-view嵌入h5 无法向h5通信 一直报错的实战教程也可以访问 https://www.itying.com/category-93-b0.html


在 uni-app 的 App 端使用 web-view 与 H5 页面通信,需要注意几个关键点。根据你的代码,问题可能出在以下几个方面:

  1. H5 端未正确引入 uni-app 的 SDK:H5 页面需要通过特定的方式接收来自 App 的消息。你需要在 H5 页面的 HTML 中引入 uni-webview-js 库,或者在构建时确保该 SDK 可用。这是通信的基础。

  2. 消息传递方式:在 H5 端,你使用了 webViewJs.postMessage,但 webViewJs 对象可能未正确定义或初始化。通常,H5 端应通过 uni.postMessage 来发送消息,前提是已正确引入上述 SDK。

  3. App 端消息发送时机:在 sendMessageToWeb 方法中,你通过 evalJS 执行脚本,这依赖于 H5 端的 window.handleAppMessage 函数已定义。如果 H5 页面加载未完成或函数未定义,就会报错。

  4. iOS 系统限制:iOS 对 web-view 的安全限制较严格,可能需要检查 H5 页面的 URL 是否允许跨域通信,或是否使用了 HTTPS(在本地测试中,HTTP 可能被阻止)。

建议调整

  • 在 H5 页面的 index.html 或入口文件中,添加以下脚本引入(如果使用构建工具,需确保路径正确):

    <script src="https://g.alicdn.com/dingding/dingtalk-jsapi/2.7.13/dingtalk.open.js"></script>
    <!-- 或使用本地 uni-app SDK -->
    <script type="text/javascript" src="/uni.webview.1.5.4.js"></script>
    

    具体版本号需匹配你的 uni-app 版本。

  • 在 H5 端,将 webViewJs.postMessage 改为 uni.postMessage,并确保在 mounted 后调用,例如:

    const handleSend = () => {
      if (typeof uni !== 'undefined' && uni.postMessage) {
        uni.postMessage({ data: { action: 'xxx' } });
      } else {
        console.error('uni SDK 未加载');
      }
    }
回到顶部