错误信息:
Uncaught (in promise) NavigationDuplicated: Avoided redundant navigation
to current location: "/welcome".
- 动态路由跳转的时候重复点击跳转到一个路由地址时报的错
单词
navigation
n. 航行;航海
duplicated
v. 复制;复印;(使)成倍增加;(无必要地)重复(某事)(duplicate 的过去式及过去分词)
redundant
adj. 多余的,过剩的;被解雇的,失业的;冗长的,累赘的
解决
- 解决办法1:
// 加入在main.js
import Router from 'vue-router'
const routerPush = Router.prototype.push
Router.prototype.push = function push(location) {
return routerPush.call(this, location).catch(error=> error)}
- 解决办法2:
npm i vue-router@3.0 -S
- 解决办法3: (高成本高收益)
-
vue-router的开发者也给出了解决方法,你需要为每个router.push增加回调函数。
router.push('/location').catch(err => {err}) -
对于我们来说这个解决方案的成本可能很高,但是是值得的。在vue-router 3.1版本之前的push调用时不会返回任何信息,假如push之后路由出现了问题也不会有任何的错误信息。如果你使用方案1固定了vue-router的版本,那么以后的项目需要路由的回调时你根本无从下手。