在刚开始的时候将小程序的入口文件直接指向tabbar 的首页,此时出现问题:二维码扫描,第一次不关闭首页,第二次进入时;不会通过onLoad过程解析scene参数;html
官方中解释:tabbar跳转方式触发的生命周期是 onShow,不通过onLoad,下图:json
此时,和小伙伴讨论重定向问题时,想到用相似的方法能够作到,就立马实行:小程序
app.json中加pages/index/index(入口文件),pages/home/home(tabbar页面主页),pages/detail/detail(详情页);pages/exclusive/exclusiveapp
在index.js中 onLoad处理:函数
/** * 生命周期函数--监听页面加载 */ onLoad: function (options) { // 入口文件 决定进入哪一个页面 console.log('入口文件,参数scene,值detail%2C1127') var scene = options.scene; //扫码进入有此参数
var scene = decodeURIComponent(options.scene); if (scene) { //'scene=detail%2C1127' 分隔符, 测试时为 , 号;真机时为%2C 缘由是url编码,可是使用decodeURI()解析不出来,因此走了兼容 let info_arr = []; info_arr = scene.split(','); //console.log(info_arr) let _type = info_arr[0]; let id = info_arr[1]; if (_type == 'detail') { wx.redirectTo({ url: `../detail/detail?id=${id}`, }) } else if (_type == 'exclusive') { wx.redirectTo({ url: `../exclusive/exclusive?id=${id}`, }) } }else{ wx.switchTab({ url: '../home/home', }) } },
此时,完美解决 从 扫码-->home-->detail;再次扫码-->home 不能到-->detail的问题;测试
此时 扫码-->index(redirectTo)-->detail;再次扫码-->index(redirectTo)-->detail的问题;越过home页面编码
因为home页面有大量的请求,不适宜用redirectTo;因此此方法算是折中的选择了url