解决vue-router路由重定向问题

Uncaught (in promise) Error: Redirected when going from "/login" to "/" via a navigation guard.

vue-router路由版本更新产生的问题,导致路由跳转失败抛出该错误。

真正的原因是由于返回了一个Promise对象,正常的跳转由then方法执行,当正常的路由跳转被"路由导航守卫"拦截并重新指定路由时,由于 this.$router.push() 返回的是Promise对象,此时then方法不能正常执行,无法跳转到指定路由,就触发了该对象的捕获错误的方法,throw抛出错误,但并不影响程序功能。

解决方式:

通过重写VueRouter原型对象上的push方法。一定要在router创建实例之前。

import Router from "vue-router";

const originPush = Router.prototype.push;
const originReplace = Router.prototype.replace;

Router.prototype.push = function(location, resolve, reject) {
  if (resolve && reject) {
    originPush.call(this, location, resolve, reject);
  } else {
    originPush.call(this, location, () => {}, () => {});
  }
}
Router.prototype.replace = function(location, resolve, reject) {
  if (resolve && reject) {
    originReplace.call(this, location, resolve, reject);
  }else {
    originReplace.call(this, location, () => {}, () => {});
  }
}

Vue.use(Router);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值