路由如何改变,对我的案子很重要.

这就是我所拥有的:

router.beforeEach((to, from, next) => {
  if ( /* IsItABackButton && */ from.meta.someLogica) {
    next(false) 
    return ''
  }
  next()
})

是否有一些内置的解决方案,我可以使用,而不是IsItABackButton comments ?我想Vue路由本身并没有,但任何解决方法都可以在这里使用.还是会有其他更好的方式来识别它?

推荐答案

这是我找到的唯一方法:

我们可以监听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()
})

Vue.js相关问答推荐

使用项目和字段,列跨度为 2 的 Bootstrap-vue 表

如何将外部 svg 转换为 Vue3 组件?

vue3 ssr 不返回纯 html

组合 API | Vue 路由参数没有被监视

Vuejs - 更新数组中对象的数组

vue 计算的 ant watch 不会在嵌套属性更改时触发

如何从mustache内的过滤器输出html

如何对将 DOM 元素附加到 HTML 正文的 Vue 组件进行单元测试?

VueJS中的绑定函数含义

vue.js 在 App.vue 中获取路由名称

vuejs vue-cli 如何使用 console.log 而不会出现任何错误

在下拉菜单上使用箭头键滚动

判断 vue-router.beforeEach 不限制对路由的访问

如何在 vue.js 2 的其他组件中调用方法?

不需要提交Mutations的 vuex 操作

如何在 Vue 中转发 $refs

如何让 Vue (vue cli 3) 正确处理 GraphQL 文件?

过渡后的 v-if 下方的过渡元素

克隆元素并附加到 DOM 的正确方法

$emit 不会触发子事件