关于vue-router4的路由专享守卫问题

目前技术栈用的vue3+vuerouter4,有个需求是进入某页面需要判断当前页面是否可进入,不可进入就跳转到页面a,我看router的路由专享守卫是俩参数没有next,如果我想跳转到页面a怎样写,

beforeEnter:()=>{
  if(ok){
    return true
  }
  return {name:'a'}
}

这样吗,但是我试了下发现校验失败他并没有跳转到a页面

阅读 2.6k
1 个回答

看了下beforeEnter只支持支持返回一个path的字符串比如/foo?a=b,或者{path: '', query:{}}这种对象,不支持返回route对象

参考链接
https://github.com/vuejs/vue-...

export type NavigationGuardReturn =
  | void
  | Error
  | RouteLocationRaw
  | boolean

export interface NavigationGuardWithThis<T> {
  (
    this: T,
    to: RouteLocationNormalized,
    from: RouteLocationNormalized,
    next: NavigationGuardNext
  ): NavigationGuardReturn | Promise<NavigationGuardReturn>
}

export type RouteLocationRaw =
  | string
  | (RouteQueryAndHash & LocationAsPath & RouteLocationOptions)
  | (RouteQueryAndHash & LocationAsRelativeRaw & RouteLocationOptions)

export interface _RouteRecordBase extends PathParserOptions {
   beforeEnter?:
    | NavigationGuardWithThis<undefined>
    | NavigationGuardWithThis<undefined>[]
  //...
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题