用this.$router.push()时会返回一个异常,那是因为用push或者replace时会返回一个promise对象,而并没有捕获所以有Uncaught (in promise)错误,因此我们只要捕获就好了
第一种方法是直接捕获
在push()后调用一个catch方法
this.$router.push(this.path).catch(err=>err)
第二种方法是重写原型中的push方法
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)
}
此重写代码来源于https://blog.csdn.net/haidong55/article/details/100939076?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.not_use_machine_learn_pai&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.not_use_machine_learn_pai