vue重复路由报错问题

Vue路由重复点击时会出现报错 Uncaught (in promise) NavigationDuplicated: Avoided redundant navigation to current location:XXX,虽然不影响运行结果,但最好还是处理一下。
在这里插入图片描述

局部处理

局部处理是指对单个路由进行处理,其他的路由依旧报错。

在路由跳转的push语句中添加catch捕捉错误

所以原来的路由跳转改为以下代码:

		toecharts(){
            this.$router.push({
                    path:'/echarts'
                }
            ).catch(err => {})		//添加catch捕捉错误
        }

全局处理

全局处理是指对所有路由进行处理。

在main.js文件添加以下代码(代码直接复制就好,无需改动)

import Router from 'vue-router'

const originalPush = Router.prototype.push
Router.prototype.push = function push(location) {
	return originalPush.call(this, location).catch(err => err)
}

猜你喜欢

转载自blog.csdn.net/Qhx20040819/article/details/132642585
今日推荐