項目是nuxt的,雖然vue-router的模式是history,但是在ios里有個問題:當路由變化時,微信瀏覽器的url不改變,導致從微信瀏覽器跳轉到手機瀏覽器時,不能打開指定頁面。
解決方案:在全局路由守衛后置鉤子(afterEach)里,主動修改url
// plugin/route.js
export default ({ app }) => {
app.router.afterEach((to, from) => {
const u = navigator.userAgent.toLowerCase()
if(u.indexOf("like mac os x") < 0 || u.match(/MicroMessenger/i) != 'micromessenger') return
if (to.path !== global.location.pathname) {
location.assign(to.fullPath)
}
})
}
之所以用全局后置鉤子,但是有個頁面用了組件內的前置鉤子,二者有沖突,導致該頁面打不開。
而全局后置鉤子在路由守衛順序中處于最后一個階段,所以用起來沒問題。