Vue 路由报错Navigating to current location ("/postlist/") is not allowed
问题
在Vue 项目中,通过编程式导航的push 导航到页面时报错:Navigating to current location ("/postlist/") is not allowed,意思是不允许导航到当前位置。
解决办法
局部解决
在报错的push 语句后面增加catch ,例如:
this.$router.push({ path: '/postlist/', query: { categories: cid } }).catch(err => err)
这样修改后,就不会再报上面的错误了。
全局解决
在导入vue-router之后,重写push方法增加catch 。具体就是在src/router/index.js 里面import Router from 'vue-router' 之后进行修改,例如:
import VueRouter from 'vue-router'
const stdPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
return stdPush.call(this, location).catch(error=> error)
}
冀公网安备 13010402001886号