onShareAppMessage分享分包頁面,點進分享卡片進入小程序后路由未保留,被分享的頁面變成了首頁,導致返回鍵失效
處理方式:
做一個全局路由攔截,路由被清空時返回小程序首頁
創(chuàng)建一個攔截器interceptor.js:
// 路由攔截
function addRouteInterceptor() {
// 增加一個返回鍵的攔截,從分享頁面進入小程序,路由只有一級,這時點返回使其回到首頁
uni.addInterceptor("navigateBack", {
invoke(e) {
let routes = getCurrentPages()
// console.log('navigateBack', routes)
if (routes.length > 1) {
return true
} else {
uni.reLaunch({
url: '/pages/index/index'
})
return false
}
}
})
}
export default {
addRouteInterceptor,
}
如果路由棧只有一個,那就重定向到首頁
在App.vue的onLaunch里注冊下路由攔截器
export default {
onLaunch: function(options) {
// 路由攔截
interceptor.addRouteInterceptor()
},
}