vue2: vue-router@3.6.3 报错 Uncaught (in promise) NavigationDuplicated

一、原因

路由跳转有两种形式:

  • 声明式<router-link :to="...">
  • 编程式router.push(...)

声明式导航是没有这种问题的,这是因为 vue-router 底层已经处理好了。

而编程式导航连续点击进行路由跳转的时候,就会在控制台抛出 Uncaught (in promise) NavigationDuplicated 错误信息,错误信息如下:

 

 这是因为 vue-router 版本升级到了 v3.1.0 之后,push 和 replace 方法会返回一个 promise,如果没有对错误进行捕获,那么控制台就会输出未捕获异常。

2 解决方法(四种方法)

方法一:使用时对错误进行捕获。

this.$router.push('home').catch(err => { return err })

方法二: 使用时给 push 传递相应的成功、失败的回调函数。

this.$router.push({ path: 'home' }, () => {}, () => {})

方法三:降低 vue-router 版本为 3.0。

npm install vue-router@3.0

方法四:

重写 push 和 replace 方法,只需要在 router 文件夹下的 index.js 文件里面:

vue.use(VueRouter)后面加入如下代码即可。

Vue.use(VueRouter)
let originalPush = VueRouter.prototype.push;
let originalReplace = VueRouter.prototype.replace;

VueRouter.prototype.push = function push(location, resolve, reject) { //重写push解决升级route之后,重复点跳转路由error
    if (resolve && reject) {
        originalPush.call(this, location, resolve, reject);
    } else {
        // originalPush.call(this, location).catch(err => err)
        originalPush.call(this, location, () => {
        }, () => {
        });
    }
}
VueRouter.prototype.replace = function replace(location, resolve, reject) { //重写push解决升级route之后,重复点跳转路由error
    if (resolve && reject) {
        originalReplace.call(this, location, resolve, reject);
    } else {
        // originalPush.call(this, location).catch(err => err)
        originalReplace.call(this, location, () => {
        }, () => {
        });
    }
}

项目的话,多个地方要用到编程式路由的话,建议选择方法4,可以一劳永逸。减少代码量

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
npm ERR! code ERESOLVE npm ERR! ERESOLVE could not resolve npm ERR! npm ERR! While resolving: vue-router@4.2.1 npm ERR! Found: vue@2.7.14 npm ERR! node_modules/vue npm ERR! peerOptional vue@"*" from @vue/babel-preset-jsx@1.4.0 npm ERR! node_modules/@vue/babel-preset-jsx npm ERR! @vue/babel-preset-jsx@"^1.0.0" from @vue/babel-preset-app@3.12.1 npm ERR! node_modules/@vue/babel-preset-app npm ERR! @vue/babel-preset-app@"^3.12.1" from @vue/cli-plugin-babel@3.12.1 npm ERR! node_modules/@vue/cli-plugin-babel npm ERR! dev @vue/cli-plugin-babel@"^3.10.0" from the root project npm ERR! vue@"^2.7.14" from the root project npm ERR! 1 more (vuex) npm ERR! npm ERR! Could not resolve dependency: npm ERR! peer vue@"^3.2.0" from vue-router@4.2.1 npm ERR! node_modules/vue-router npm ERR! vue-router@"^4.2.1" from the root project npm ERR! npm ERR! Conflicting peer dependency: vue@3.3.4 npm ERR! node_modules/vue npm ERR! peer vue@"^3.2.0" from vue-router@4.2.1 npm ERR! node_modules/vue-router npm ERR! vue-router@"^4.2.1" from the root project npm ERR! npm ERR! Fix the upstream dependency conflict, or retry npm ERR! this command with --force or --legacy-peer-deps npm ERR! to accept an incorrect (and potentially broken) dependency resolution. npm ERR! npm ERR! npm ERR! For a full report see: npm ERR! C:\Users\11833\AppData\Local\npm-cache\_logs\2023-05-29T06_00_03_754Z-eresolve-report.txt npm ERR! A complete log of this run can be found in: C:\Users\11833\AppData\Local\npm-cache\_logs\2023-05-29T06_00_03_754Z-debug-0.log
05-30

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值