Vue-router 不允许导航到当前位置("/path")错误原因以及修复方式

本文详细解析了Vue-router中导航至当前位置的错误原因,通过实例演示了错误发生的情景,并提供了三种解决方法,包括错误捕获、全局错误处理及优化跳转逻辑。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Vue-router 不允许导航到当前位置(/path)

报错提示

Navigating to current location ("/path") is not allowed
Navigating to current location ("/path") is not allowed

错误原因

控制台出现以上这种 Navigating to current location ("/path") is not allowed 时,是因为重复进入了相同路由。

错误演示

为了演示报错,用vue-cli重新构建了一个新的项目,只编写了一个按钮一个input
code:

<!-- vue模板代码 -->
<div>
	<button @click="gotoHandle">测试路由跳转</button>
	<input v-model="routeName">
<div>
// 路由跳转代码
export default {
  data() {
    return {
      routeName: ''
    }
  },
  methods: {
    gotoHandle() {
      this.$router.push({name: this.routeName})
    }
  }
}

动图演示
Navigating to current location ("/path") is not allowed
在重复进入相同路由时(不论是通过路径,还是路由名称进入),会提示不允许导航到当前位置(path), 就像上面的例子进入路由名为About的路由时,提示的是path: "/about"Navigating to current location ("/about") is not allowed。这是因为跳转的方法错误时,未捕获错误处理,因此直接输出了错误信息。

解决方法

方法一

直接在跳转报错的那个地方加上.catch(error => error)

export default {
  data() {
    return {
      routeName: ''
    }
  },
  methods: {
    gotoHandle() {
      this.$router.push({name: this.routeName}).catch(error => error)
    }
  }
}

方法二

为跳转错误的方法全局加上错误捕获。

import VueRouter from 'vue-router'

const routerPush = VueRouter.prototype.push
VueRouter.prototype.push = function (location) {
  return routerPush.call(this, location).catch(error => error)
}

以上代码在main.js,或者router/index.js 下执行,以及new VueRouter之前之后都一样。因为是重置的VueRouter原型对象上的push事件,给原型对象的push事件添加上了捕获异常,所以会通过原型链改变所有相关对象。

replace 方法重复跳转错误与上方类似,把push改成replace就好。

方法三

此方法为建议方法,建议优化跳转逻辑,避免重复跳转相同路由。

以上如果错误或疏漏,欢迎指正。

ERROR in D:/project-sky-admin-vue-ts3/src/components/Breadcrumb/index.vue 27:10 Module '"../../../node_modules/vue-router/types"' has no exported member 'RouteRecord'. 25 | import pathToRegexp from 'path-to-regexp' 26 | import { Component, Vue, Watch } from 'vue-property-decorator' > 27 | import { RouteRecord, Route } from 'vue-router' | ^ 28 | 29 | @Component({ 30 | 'name': 'Breadcrumb' ERROR in D:/project-sky-admin-vue-ts3/src/components/Breadcrumb/index.vue 27:23 Module '"../../../node_modules/vue-router/types"' has no exported member 'Route'. 25 | import pathToRegexp from 'path-to-regexp' 26 | import { Component, Vue, Watch } from 'vue-property-decorator' > 27 | import { RouteRecord, Route } from 'vue-router' | ^ 28 | 29 | @Component({ 30 | 'name': 'Breadcrumb' ERROR in D:/project-sky-admin-vue-ts3/src/layout/components/Sidebar/SidebarItem.vue 64:10 Module '"../../../../node_modules/vue-router/types"' has no exported member 'Route'. 62 | import { Component, Prop, Vue } from 'vue-property-decorator' 63 | import { UserModule } from '@/store/modules/user' > 64 | import { Route, RouteConfig } from 'vue-router' | ^ 65 | import { isExternal } from '@/utils/validate' 66 | import SidebarItemLink from './SidebarItemLink.vue' 67 | ERROR in D:/project-sky-admin-vue-ts3/src/layout/components/Sidebar/SidebarItem.vue 64:17 Module '"../../../../node_modules/vue-router/types"' has no exported member 'RouteConfig'. 62 | import { Component, Prop, Vue } from 'vue-property-decorator' 63 | import { UserModule } from '@/store/modules/user' > 64 | import { Route, RouteConfig } from 'vue-router' | ^ 65 | import { isExternal } from '@/utils/validate' 66 | import SidebarItemLink from './SidebarItemLink.vue' 67 | ERROR in D:/project-sky-admin-vue-ts3/src/permission.ts 5:10 Module '"../node_modules/vue-router/types"' has no exported member 'Route'. 3 | import 'nprogress/nprogress.css' 4 | import { Message } from 'element-ui' > 5 | import { Route } from 'vue-router' | ^ 6 | import { UserModule } from '@/store/modules/user' 7 | import Cookies from 'js-cookie' 8 | ERROR in D:/project-sky-admin-vue-ts3/src/views/login/index.vue 54:10 Module '"../../../node_modules/vue-router/types"' has no exported member 'Route'. 52 | <script lang="ts"> 53 | import { Component, Vue, Watch } from 'vue-property-decorator' > 54 | import { Route } from 'vue-router' | ^ 55 | import { Form as ElForm, Input } from 'element-ui' 56 | import { UserModule } from '@/store/modules/user' 57 | import { isValidUsername } from '@/utils/validate' Version: typescript 3.6.2 Time: 5695ms App running at: - Local: http://localhost:8888/ - Network: http://0.0.0.0:8888/
最新发布
08-01
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值