Uncaught (in promise) Error: Redirected when going from “xx“ to “xx“ via a navigation guard.

原因

首先说说出现Uncaught (in promise) Error: Redirected when going from “xx“ to “xx“ via a navigation guard.的原因,用sessionStorage对路由做了全局前置守卫,在点击未有权限的路由时,就会出现以上错误。

解决办法

方法一:

router/index.js中加入

const originalPush = VueRouter.prototype.push
VueRouter.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)
}

方法二:

在业务页面,this.$router.push操作里面做catch,例:

前:

this.$router.push('/home')

后:

this.$router.push('/home').catch(() => {
  this.$message({
    message: '请先登录!',
    type: 'info'
  })
})

方法三:

网上有其他方法说是修改@vue/cli-plugin-router或router的版,我用的是@vue/cli-plugin-router,修改为最高版本后还是有错误,大家可以测试下修改版本是否可行。

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注