学无先后,达者为师

网站首页 Vue 正文

vue重复点击导航时,控制台出现报错、Uncaught (in promise) NavigationDuplicated: Avoided redundant navigation to

作者:web半晨 更新时间: 2022-06-05 Vue


1、方案一

在router文件夹下的index.js文件中添加如下代码。

Vue.use(Router)
const router = new Router({
	routes
});
 
// 添加的的代码
const VueRouterPush = Router.prototype.push;
Router.prototype.push = function push (to) {
	return VueRouterPush.call(this, to).catch(err => err);
};
// 添加的的代码

2、方案二

在跳转时,判断跳转路由和当前路由是否一致,避免重复跳转产生问题。

toMenu (item) {
	if (this.$route.path !== item.url) {
		this.$router.push({ path: item.url });
	}
}

3、方案三

使用catch方法捕获router.push异常。

this.$router.push(route).catch(err => {
	console.log('输出报错', err);
});

4、方案四

降低路由版本。

卸载原来的版本

npm uninstall vue-router

安装指定版本

npm install vue-router@3.0.6

5、总结

方案亲测多次都没有用,最靠谱的是方案,其他方案没测。


6、相关链接

1、解决Vue报错:Uncaught (in promise) NavigationDuplicated: Avoided redundant navigation to current location

原文链接:https://blog.csdn.net/weixin_51157081/article/details/124521342

栏目分类
最近更新