解决"Uncaught (in promise) Error: Navigation cancelled from "/" to "/login" with a new navigation"报错处理

 Uncaught (in promise) Error: Navigation cancelled from “/” to “/login” with a new navigation.

这个错误是vue-router内部错误,没有进行catch处理,导致的编程式导航跳转问题,往同一地址跳转时会报错的情况。

push和replace 都会导致这个情况的发生。

 

解决方法如下:

在路由器中进行配置:router/index.js

import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)
// 解决报错
const originalPush = Router.prototype.push
const originalReplace = Router.prototype.replace
// 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)
}
// replace
Router.prototype.replace = function push (location, onResolve, onReject) {
  if (onResolve || onReject) return originalReplace.call(this, location, onResolve, onReject)
  return originalReplace.call(this, location).catch(err => err)
}

 

仅在此记录一下遇到的问题和最后的解决办法,亲测可用。

 

posted @ 2021-01-06 11:47  lpmou  阅读(5286)  评论(0编辑  收藏  举报
返回顶部