位置:- 正文

vue-router.esm.js?a12b:2046 Uncaught (in promise) NavigationDuplicated: Avoided redundant navigation

编辑:rootadmin
vue-router.esm.js?a12b:2046 Uncaught (in promise) NavigationDuplicated: Avoided redundant navigation 报错信息

推荐整理分享vue-router.esm.js?a12b:2046 Uncaught (in promise) NavigationDuplicated: Avoided redundant navigation,希望有所帮助,仅作参考,欢迎阅读内容。

文章相关热门搜索词:,内容如对您有帮助,希望把文章链接给更多的朋友!

vue-router.esm.js?a12b:2046 Uncaught (in promise) NavigationDuplicated: Avoided redundant navigation to current location: "/home".

报错原因:重复点击路由导致,因为vue-router引入了promise,当我们使用this.$router.push时候需要多添加成功或失败的回调,否则就会报出以上的错误。

原代码

 解决办法

第一种

        在进行路由跳转时对路径进行判断如果重复即不再执行

switchTab(path) {if (this.$router.path == path) returnthis.$router.push(path)}

第二种

        跳转后使用catch语句对错误不再进行处理

this.$router.push(path).catch(err => {})

第三种

        基于第二种方法,当我们的路由跳转过多时就需要每次跳转都要加上catch回调,这样比较麻烦,所以还可以在引入vueRouter文件下重写push和replace方法

import VueRouter from 'vue-router';//保存原型对象的Pushlet originPush = VueRouter.prototype.pushlet originReplace = VueRouter.prototype.replace//重写push方法VueRouter.prototype.push = function (location, res, rej) { if (res && rej) { originPush.call(this, location, res, rej) } else { originPush.call(this, location, () => { }, () => { }) }}//重写replace方法VueRouter.prototype.replace = function (location, res, rej) { if (res && rej) { originReplace.call(this, location, res, rej) } else { originReplace.call(this, location, () => { }, () => { }) }} vue-router.esm.js?a12b:2046 Uncaught (in promise) NavigationDuplicated: Avoided redundant navigation

csdn文章推荐受影响解决办法10个字10行

csdn文章推荐受影响解决办法10个字10行

csdn文章推荐受影响解决办法10个字10行

csdn文章推荐受影响解决办法10个字10行

csdn文章推荐受影响解决办法10个字10行

csdn文章推荐受影响解决办法10个字10行

csdn文章推荐受影响解决办法10个字10行

csdn文章推荐受影响解决办法10个字10行

csdn文章推荐受影响解决办法10个字10行

csdn文章推荐受影响解决办法10个字10行

csdn文章推荐受影响解决办法10个字10行

csdn文章推荐受影响解决办法10个字10行

csdn文章推荐受影响解决办法10个字10行

csdn文章推荐受影响解决办法10个字10行

本文链接地址:https://www.jiuchutong.com/zhishi/292331.html 转载请保留说明!
下一篇链接:https://www.jiuchutong.com/zhishi/292332.html
免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

鄂ICP备2023003026号

友情链接: 武汉网站建设 电脑维修 湖南楚通运网络