在做微信小程序开发时,页面跳转传参两种方法简单记录下
1、通过url拼接需要传递的参数
保留当前页面,跳转到应用内的某个页面
uni.navigateTo({
url:"test?id=1"
})
然后在跳转后的页面中通过onLoad()中接收参数(onLoad()方法在页面打开时只执行一次)
onLoad: function(option) {
console.log('onLoad', option.id);
}
2、通过eventChannel向被打开页面传送数据
uni.navigateTo({
url:"test",success: function(res) {
res.eventChannel.emit('testValue', {
id: 1
})
}
})
监听acceptDataFromOpenerPage事件,获取上一页面通过eventChannel传送到当前页面的数据
onLoad: function(option) {
const eventChannel = that.getOpenerEventChannel()
eventChannel.on('testValue', function(data) {
console.log('data',data);
})
},