解决 vue-router 升级导致的 Uncaught (in promise) NavigationDuplicated
解决 vue-router 升级导致的 Uncaught (in promise) NavigationDuplicated
1 导致错误原因
路由跳转有两种形式:
- 声明式:
<router-link :to="...">
- 编程式:
router.push(...)
声明式导航是没有这种问题的,这是因为 vue-router 底层已经处理好了。
而编程式导航连续点击进行路由跳转的时候,就会在控制台抛出 Uncaught (in promise) NavigationDuplicated 错误信息,错误信息如下:
这是因为 vue-router 版本升级到了 v3.1.0 之后,push 和 replace 方法会返回一个 promise,如果没有对错误进行捕获,那么控制台就会输出未捕获异常。
2 解决方法
- 使用时对错误进行捕获。
this.$router.push('about').catch(err => { return err })
- 使用时给 push 传递相应的成功、失败的回调函数。
this.$router.push({ path: 'about' }, () => {}, () => {})
- 降低 vue-router 版本为 3.0。
npm install vue-router@3.0
- 重写 push 和 replace 方法,只需要在 router 文件夹下的 index.js 文件里面加入如下代码即可。
import VueRouter from 'vue-router'// 解决 vue-router 升级导致的 Uncaught(in promise)
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push (location) {return originalPush.call(this, location).catch(err => err)
}const originalReplace = VueRouter.prototype.replace
VueRouter.prototype.replace = function replace (location) {return originalReplace.call(this, location).catch(err => err)
}Vue.use(VueRouter)
注意:
VueRouter.prototype.push
中的VueRouter
要与import VueRouter from 'vue-router'
中的VueRouter
名称一致并且要写在Vue.use(VueRouter)
之前。
解决 vue-router 升级导致的 Uncaught (in promise) NavigationDuplicated相关推荐
- vue router 报错: Uncaught (in promise) NavigationDuplicated {_name:““NavigationDuplicated“... 的解决方法
报错 方法一.更换版本 npm i vue-router@3.0 -S 方法二 修改入口文件 //main.js import Router from 'vue-router'const origin ...
- 解决Vue报错:Uncaught (in promise) NavigationDuplicated: Avoided redundant navigation to current location
问题描述:重复点击导航时,控制台出现报错 ,虽然不影响功能使用,但也不能坐视不管. 解决 Vue 重复点击相同路由,出现 Uncaught (in promise) NavigationDuplica ...
- Vue的router导航重复-报错:Uncaught (in promise) NavigationDuplicated: Avoided redundant navigation to curren
报错: Uncaught (in promise) NavigationDuplicated: Avoided redundant navigation to current location: &q ...
- vue报错 Uncaught (in promise) NavigationDuplicated {_name:““NavigationDuplicated“... 的解决方法
vue报错 Uncaught (in promise) NavigationDuplicated {_name:""NavigationDuplicated"... 的解 ...
- 解决Uncaught (in promise) NavigationDuplicated: Avoided redundant navigation to current location问题
在Vue路由中遇到Error: Avoided redundant navigation to current location:xxxx:错误,意思是路由重复 在router文件夹下的index.j ...
- 解决报错:Uncaught (in promise) NavigationDuplicated: Avoided redundant navigation to current location: “
报错内容: vue-router.esm.js?68cf:2065 Uncaught (in promise) NavigationDuplicated: Avoided redundant navi ...
- 解决路由报错Uncaught (in promise) NavigationDuplicated:
Uncaught (in promise) NavigationDuplicated: Avoided redundant navigation to current location: " ...
- vue报错Uncaught (in promise) NavigationDuplicated:Avoided redundant navigation
vue-router.esm.js?3423:2065 Uncaught (in promise) NavigationDuplicated: Avoided redundant navigation ...
- vue发送请求时遇到Uncaught (in promise) TypeError Cannot read properties of undefined(reading ‘randomExtend
最近在项目中遇到一个前端问题Uncaught (in promise) TypeError: Cannot read properties of undefined(reading 'randomEx ...
最新文章
- [转]安装和使用JD-Eclipse插件
- tcpdump的简单选项介绍
- 面试必备:30 个 Java 集合面试问题及答案
- Struts2的国际化(一)-国际化资源文件的配置及国际化信息的访问
- 粤教版管理计算机中的文件教案,最新粤教版初中信息技术第一册教案(全册).docx...
- 代码缺乏装饰?使用ts装饰器来装饰你的代码
- require.context
- unity3d生命周期
- Maven学习(三)-----Maven本地资源库
- 编译原理c语言递归下降程序,编译原理(递归下降分析程序)
- 软件测试周刊(第25期):不要成天到晚地找意义
- 手机电视-CMMB系统技术应用浅谈
- SQL Server 2005“错误1706。安装程序找不到需要的文件。请检查……”的处理办法
- 量化交易2-backtrader导入回测数据
- English Songs
- c语言流水灯程序 16,16个发光二极管流水灯程序
- 主流操作系统的开发语言
- 使用SVG路径图制作线追踪特效
- 尚学堂Java学习笔记
- SQL Server安全(4/11):许可(Permissions)
热门文章
- Kylin cuboid算法修改
- Linux防火墙开放端口
- 怎样清理苹果电脑磁盘空间_Macbook苹果电脑提示磁盘空间不足怎么办【解决方法】...
- 5年大厂Java高频面试题及答案整理
- 【C#/WPF】TextBlock/TextBox/Label编辑文字的问题
- yield关键字用法
- 黑鲨3pro手机如何升级鸿蒙5g系统,黑鲨3 Pro升降按键怎么进行设置
- webView显示白屏的问题
- BuildDownload maven-metadata.xml...打包一直卡在这里
- Linux文件系统:minix文件系统二进制分析3(硬链接与软链接)