美文网首页vueweb前端
vue-router报错Uncaught (in promise

vue-router报错Uncaught (in promise

作者: 码农界四爷__King | 来源:发表于2020-04-27 22:07 被阅读0次

1、报错原因:

在升级了Vue-Router版本到到3.1.0及以上之后,页面在跳转路由控制台会报Uncaught (in promise)的问题,在3.1.0版本里面新增功能:push和replace方法会返回一个promise, 你可能在控制台看到未捕获的异常。

image

2、解决方法:

a、在调用方法的时候用catch捕获异常

this.$router.replace('/home').catch(err => {
   console.log(err)
})

b、对Router原型链上的push、replace方法进行重写,这样就不用每次调用方法都要加上catch

在router.js加入以下内容:

import Router from 'vue-router'
  
const originalPush = Router.prototype.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)
}

相关文章

网友评论

    本文标题:vue-router报错Uncaught (in promise

    本文链接:https://www.haomeiwen.com/subject/zhspwhtx.html