您当前的位置: 首页 >  ar

【03】

暂无认证

  • 1浏览

    0关注

    196博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

vue路由报错Navigation aborted from “/a“ to “/b“ via a navigation guard.两种解决方案

【03】 发布时间:2020-08-18 19:51:04 ,浏览量:1

此情此景,报错
router.beforeEach((to, from, next) => {
	next(false)
}

或者没执行next()

router.beforeEach((to, from, next) => {
	if(false){
	next()
	}
}

官方文档所说

  • xt: Function: 一定要调用该方法来 resolve 这个钩子。执行效果依赖 - xt 方法的调用参数。

  • xt(): 进行管道中的下一个钩子。如果全部钩子执行完了,则导航的状态就是 confirmed (确认的)。

  • xt(false): 中断当前的导航。如果浏览器的 URL 改变了 (可能是用户手动或者浏览器后退按钮),那么 URL 地址会重置到 from 路由对应的地址。

  • xt(’/’) 或者 next({ path: ‘/’ }): 跳转到一个不同的地址。当前的导航被中断,然后进行一个新的导航。你可以向 next 传递任意位置对象,且允许设置诸如 replace: true、name: ‘home’ 之类的选项以及任何用在 router-link 的 to prop 或 router.push 中的选项。

  • xt(error): (2.4.0+) 如果传入 next 的参数是一个 Error 实例,则导航会被终止且该错误会被传递给 router.onError() 注册过的回调。

官方文档地址https://router.vuejs.org/zh/guide/advanced/navigation-guards.html#%E5%85%A8%E5%B1%80%E5%89%8D%E7%BD%AE%E5%AE%88%E5%8D%AB

分析

很显然这并不是代码问题,很可能是官方bug

解决

方案1:将vue-router版本调整至3.0.7 方案2:在 route.js 下补充以下代码

const originalPush = Router.prototype.push
Router.prototype.push = function push (location, onResolve, onReject) {
  if (onResolve || onReject){
    return originalPush.call(this, location, onResolve, onReject)
  }
  return originalPush.call(this, location).catch(err => err)
}

关注
打赏
1657344724
查看更多评论
立即登录/注册

微信扫码登录

0.0413s