解决vue项目路由出现message: "Navigating to current location (XXX) is not allowed"的问题

1,489 阅读1分钟
错误代码:

NavigationDuplicated {_name: "NavigationDuplicated", name: "NavigationDuplicated", message: "Navigating to current location ("/index") is not allowed"

操作:

在VUE-cli4搭建的项目中点击两次路由切换

原因:

在路由跳转的时候同一个路由多次添加是不被允许的

解决方案:在你引了vue-router的js文件里加上如下代码即可
import Vue from 'vue'  //如果已引用,不需要重复引用
import Router from 'vue-router'; //如果已引用,不需要重复引用
Vue.use(Router) //如果已引用,不需要重复引用
const routerPush = Router.prototype.push 
Router.prototype.push = function push (location) {
    return routerPush.call(this, location).catch(err => err)
}