行业知识
H5页面向小程序发消息
2026-11-08 17:18:43
来源:互联网转载
1.通过postMessage来传递消息
在H5中需要发消息时,调用wx.miniProgram.postMessage方法发送消息,在webview中定义@message事件接收消息
注意:仅会在特定时机(后退、组件销毁、分享,复制链接)触发并收到消息
//H5页面
我要发消息给小程序
postToWx1(){
//向小程序发送消息
wx.miniProgram.postMessage({
data:{
key:'leeee'
}
})
},
//小程序页面A
getmessage(e){
//获取最新的一条消息
const data = e.detail.data[e.detail.data.length - 1]
console.log('getMsg',data);
console.log('key:',data.key);
this.$utils.alert('key:'+data.key)
},
3.2绕点远路,页面跳转
假设上述小程序页面为A,H5页面为a
上一篇:多文件上传功能强大源码深度解析