检测Vue

编程入门 行业动态 更新时间:2024-10-20 16:41:49
本文介绍了检测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

本文发布于:2023-10-12 17:04:17,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1485331.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Vue

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!