小程序自基礎(chǔ)庫 2.7.3 開始在wx.navigateTo()頁面跳轉(zhuǎn)方法中加入了events屬性,通過此屬性可以實現(xiàn)不同頁面之間的方法互相調(diào)用和傳遞數(shù)據(jù)
相關(guān)文檔鏈接:https://developers.weixin.qq.com/miniprogram/dev/api/route/wx.navigateTo.html
代碼示例:
頁面A
wx.navigateTo({
url: 'pages/pageB',
events: {
// 為指定事件添加一個監(jiān)聽器,獲取被打開頁面?zhèn)魉偷疆斍绊撁娴臄?shù)據(jù)
pageDataB: function(data) {
console.log('頁面B觸發(fā)事件時傳遞的數(shù)據(jù)1:',data)
},
someEvent: function(data) {
console.log('頁面B觸發(fā)事件時傳遞的數(shù)據(jù)2:',data)
}
}
})
頁面B
onLoad: function(){
const eventChannel = this.getOpenerEventChannel()
// 通過觸發(fā)相關(guān)事件傳遞數(shù)據(jù)
eventChannel.emit('pageDataB', {toPageA: '這是發(fā)送到頁面A的數(shù)據(jù)1'});
eventChannel.emit('someEvent', {data: '這是發(fā)送到頁面A的數(shù)據(jù)2'});
}