Vue-Router升级导致的Uncaught ,in promise问题【转载别人的】

这是什么原因呢?

看vue-router的版本更新日志

V3.1.0版本里面新增功能:push和replace方法会返回一个promise, 你可能在控制台看到未捕获的异常

解决方法一:在调用方法的时候用catch捕获异常

this.$router.replace({ name: 'foo' }).catch(err => {

console.log('all good')

})

方法二: 对Router原型链上的push、replace方法进行重写,这样就不用每次调用方法都要加上catch。这个方法是vue-router的issues里面的一位大佬解决的

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)

}

————————————————

版权声明:本文为CSDN博主「只想做个好人啊」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。

原文链接:https://blog.csdn.net/haidong55/article/details/100939076