Vue-Router:Uncaught (in promise) 报错处理

  • Vue-router≥3.0版本中,路由回调形式已经改成了Promise API形式,返回一个Promise

  • 在相同路由转跳并且没有捕获错误时会在控制打印如下信息:

  • 方法1:可在全局配置一个catch捕捉错误:

    我是直接写在 router > index.js 文件中

      // 包装路由函数,添加处理错误方法;
          const originalPush = VueRouter.prototype.push;
          VueRouter.prototype.push = function(location) {
              return originalPush.call(this, location).catch(err => err);
          };
    
  • 方法2:在调用路由函数时捕获错误:

  • 方法3:使用3.0以下版本。

posted @ 2020-04-23 23:51  Wuxiaoyi  阅读(1911)  评论(0编辑  收藏  举报