小程序 EventChannel 页面间事件通信通道
小程序除了通过页面跳转传递query
参数,还可以通过EventChannel
传递数据
触发事件——当前页面
wx.navigateTo({
url: '../detail/index',
success: function (res) {
// 通过eventChannel向被打开页面传送数据
res.eventChannel.emit('handleEvent', {detail}) //触发事件
}
})
监听事件——跳转页面
onLoad: function (options) {
let that = this;
const eventChannel = this.getOpenerEventChannel(); //取到事件对象
eventChannel.on("handleEvent",data=>{//发布事件
that.setData({
cartDataList: data.detail//cartDataList为此页的数据模型
});
console.log(that.data.detail,"我被传过来了");
});
console.log(eventChannel);
},