vue 当前路由重复点击跳转报错

当在Vue应用中连续点击同一路由时,可能会出现路由报错,但不影响页面运行。为消除这个错误提示,可以尝试在router配置中添加特殊处理或在菜单跳转时判断,如果路由相同则先跳转到刷新页面再返回原页面,实现当前路由的重新加载。

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

当菜单路由重复点击时,会出现以下报错:
随便找了个项目搞了个报错
虽然没有什么影响(不知道深层有什么影响,页面运行反正没啥问题),但是有个报错看着就毕竟闹心。
有以下解决方案:

  1. router配置中添加代码块不再报错
// 解决Vue-Router升级导致的Uncaught(in promise) navigation guard问题
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location, onResolve, onReject) {
  if (onResolve || onReject)
    return originalPush.call(this, location, onResolve, onReject);
  return originalPush.call(this, location).catch((err) => err);
};
  1. 增加刷新页面,重新加载当前页
    刷新页面
<!-- 空页面,负责中转到目标页面 -->
<template>
  <div></div>
</template>
 
<script>
export default {
  name: 'refresh',
  data() {
    return {};
  },
  beforeRouteEnter(to, from, next) {
    next((vm) => {
      vm.$router.replace(from.path);
    });
  },
};
</script>

路由

{
  path: "/refresh",
  name: "refresh",
  hideInMenu: true,
  component: () =>
    import( /* webpackChunkName: "login" */ "@/components/refresh/refresh"),
},

菜单跳转时

 routeJump(item) {
      const toPath = item.path;
      const thePath = this.$route.path;
      if (toPath == thePath) {
        this.$router.replace({ path: '/refresh' });
      } else {
        this.$router.push({ name: item.name });
      }
  },

如果当前路由与跳转路由相同,则跳转到refresh页面中,再跳回原页面,重新加载一次当前路由。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值