本文介绍了检测Vue-Router导航卫士中的返回按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
路线如何改变,对我来说很重要.因此,我想通过浏览器或 gsm 的后退按钮更改路线.
How the route is changed, matters for my case. So, I want to catch when the route is changed by a back button of browser or gsm.
这就是我所拥有的:
router.beforeEach((to, from, next) => { if ( /* IsItABackButton && */ from.meta.someLogica) { next(false) return '' } next() })是否有一些我可以使用的内置解决方案来代替 IsItABackButton 注释?我猜 Vue-router 本身没有,但任何解决方法也可以在这里工作.或者还有其他更喜欢的识别方式吗?
Is there some built-in solutions that I can use instead of IsItABackButton comment? Vue-router itself hasn't I guess but any workaround could also work here. Or would there be another way preferred to recognize it?
推荐答案这是我找到的唯一方法:
This is the only way that I've found:
我们可以监听 popstate,将其保存在一个变量中,然后检查该变量
We can listen for popstate, save it in a variable, and then check that variable
// 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() })更多推荐
检测Vue
发布评论