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);