当前位置:   article > 正文

vue2: Router报错经验--Uncaught (in promise) Error: Navigation cancelled from“/mall“to “/home“ with a new_error: navigation cancelled from "/" to "/home" wi

error: navigation cancelled from "/" to "/home" with a new navigation.

vue router报错

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

原因

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

在这里插入图片描述

方法一(重写push)

const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch((err) => err);
};

  • 1
  • 2
  • 3
  • 4
  • 5

方法二 在router的index.js中插入以下代码解决:

const originalPush = VueRouter.prototype.push;
const originalReplace = VueRouter.prototype.replace;
//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);
};
//replace
VueRouter.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);
};
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/224697
推荐阅读
相关标签
  

闽ICP备14008679号