在剛開始的時候將小程序的入口文件直接指向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