
学做Vue项目,发现在连续点击同一个tabBarItem即点击同一个Url的链接后,会出现报错在点击同一个URL的时候,会报错 Uncaught (in promise,截图如下:

这个问题是vue-routerV3.1.0版本里面新增功能:push和replace方法会返回一个promise, 你可能在控制台看到未捕获的异常
解决方案:
1、简单粗暴,直接降级,在项目目录下运行
npm i vue-router@3.0 -S
2、捕获异常或直接对Vue原型PUSH或replace重写
方法一:在调用方法的时候用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)
原文链接:https://blog.youkuaiyun.com/haidong55/article/details/100939076
本文解决Vue项目中连续点击同一tabBarItem导致的Uncaught in promise错误。介绍vue-routerV3.1.0版本特性及两种解决方案:降级或捕获异常。
5376

被折叠的 条评论
为什么被折叠?



