路线如何改变对于我的情况很重要。
所以,我想捕捉浏览器或 gsm 的后退按钮何时更改路线。
这就是我所拥有的:
router.beforeEach((to, from, next) => {
if ( /* IsItABackButton && */ from.meta.someLogica) {
next(false)
return ''
}
next()
})
是否有一些我可以使用的内置解决方案IsItABackButton
评论?我猜 Vue-router 本身没有,但任何解决方法也可以在这里工作。或者是否有其他更好的方式来识别它?
这是我发现的唯一方法:
我们可以监听 popstate,将其保存在变量中,然后检查该变量
// This listener will execute before router.beforeEach only if registered
// before vue-router is registered with Vue.use(VueRouter)
window.popStateDetected = false
window.addEventListener('popstate', () => {
window.popStateDetected = true
})
router.beforeEach((to, from, next) => {
const IsItABackButton = window.popStateDetected
window.popStateDetected = false
if (IsItABackButton && from.meta.someLogica) {
next(false)
return ''
}
next()
})
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)