uniapp 微信小程序 与 H5 原生的实时通讯

uniapp 微信小程序 与 H5 原生的实时通讯

1、H5 必须用原生,试过很多,只能用原生。这里是官方的 demo 链接

2、uniapp 中的那个需要做交互的文件必须是nvue.

1
2
3
4
<web-view
@message="message"
src="https://uniapp.dcloud.io/static/web-view.html"
></web-view>
1
2
3
message(event){
console.log('接收到消息',event.detail.data)
}

3、H5 中加入事件监听

1
2
3
4
5
6
7
8
9
10
11
12
13
<script
type="text/javascript"
src="https://js.cdn.aliyun.dcloud.net.cn/dev/uni-app/uni.webview.1.5.2.js"
></script>
<script>
document.addEventListener("UniAppJSBridgeReady", function () {
uni.postMessage({
data: {
action: "这是我传送的消息",
},
}); //传递的消息信息,必须写在 data 对象中。
});
</script>