- 网页与小程序之间通信,网页引入JSSDK 1.3.2
- 在web-view中绑定bindmessage事件
网页向小程序 postMessage 时,会在特定时机(小程序后退、组件销毁、分享)触发并收到消息。e.detail = { data }
<web-view src='{{url}}' bindtap='toShare' bindmessage="bindmessage"></web-view>
- 网页中使用wx.miniProgram.postMessage向小程序传递数据对象
单条数据,赋给data:wx.miniProgram.postMessage({data:xxx})
多条数据-放入对象中:wx.miniProgram.postMessage({data:{}})
bindmessage(e) {
this.setData({
title: e.detail....,
imageUrl: e.detail...
})
},
onShareAppMessage(opt) {
this.toShare();
let title = this.data.title;
let imageUrl = this.data.imageUrl;
return {
title: title,
imageUrl: imageUrl,
path: 'pages/...,
success: function (res) {
console.log(res)
},
fail: function (res) {
console.log(res)
}
}
},