重复导航到当前位置引起的。Vue Router 提供了一种机制,阻止重复导航到相同的路由路径。

代码:

html 复制代码
  <!-- 侧边栏 -->
        <el-col :span="12" :style="{ 'width': '200px' }">
          <el-menu default-active="first" class="el-menu-vertical-demo" @select="handleMenuSelect">
            <el-menu-item index="first">
              <i class="el-icon-menu"></i>
              <span slot="title">首页</span>
            </el-menu-item>
            <el-menu-item index="person">
              <i class="el-icon-menu"></i>
              <span slot="title">个人中心</span>
            </el-menu-item>
            <el-menu-item index="score">
              <i class="el-icon-document"></i>
              <span slot="title">个人成绩</span>
            </el-menu-item>
            <el-menu-item index="personal">
              <i class="el-icon-document"></i>
              <span slot="title">成绩管理</span>
            </el-menu-item>
          
            <el-menu-item index="manage">
              <i class="el-icon-setting"></i>
              <span slot="title">人员管理</span>
            </el-menu-item>
          </el-menu>
        </el-col>
<script>
export default {
  methods: {
    handleMenuSelect(index) {
      this.$router.push({ path: '/' + index });
    }
  }
};
</script>

路由:

javascript 复制代码
const routes = [
	

	{
		path: '/home',//路由地址
		name: 'home',
		component: home,//相对应的组件
		redirect: { name: "first" },
		children: [
			{
				path: '/first',
				name: 'first',
				component: first
			},
			{
				path: '/person',
				name: 'person',
				component: person
			},
			{
				path: '/personal',
				name: 'personal',
				component: personal
			},
			{
				path: '/score',
				name: 'score',
				component: score
			},
			{
				path: '/manage',
				name: 'manage',
				component: manage
			}
		]
	}

]

目录

解决方法:判断目标路径是否与当前路径相同

通过 this.$route.path 获取到当前路由的路径

javascript 复制代码
 handleMenuSelect(index) {
      const targetPath = '/' + index;
      
      // 判断目标路径是否与当前路径相同
      //通过 this.$route.path 获取到当前路由的路径
      if (this.$route.path === targetPath) {
        // 如果相同则不进行导航
        return;
      }
      
      // 否则进行导航
      this.$router.push({ path: targetPath });
    }
相关推荐
用户5757303346241 天前
深入理解 Promise:从单线程到异步流程控制的终极指南
javascript
我是伪码农1 天前
Vue 大事件管理系统
前端·javascript·vue.js
无巧不成书02181 天前
KMP适配鸿蒙开发实战|从0到1搭建可运行工程
javascript·华为·harmonyos·kmp
henry1010101 天前
DeepSeek生成的网页版小游戏 - 冰壶
前端·javascript·css·html5
哆啦A梦15881 天前
Vue3魔法手册 作者 张天禹 012_路由_(二)
前端·vue.js·typescript
哆啦A梦15881 天前
Vue3魔法手册 作者 张天禹 08_回顾TS中的-接口-泛型-自定义事件
前端·vue.js·typescript
PieroPc1 天前
2026年,我的AI编程助手使用心得(纯个人体验,非评测)
javascript·css·html·fastapi·ai编程
xjf77111 天前
TypDom框架分析
javascript·typescript·前端框架·typedom
芭拉拉小魔仙1 天前
企业级Vue项目的状态管理:从原理到实战架构
前端·vue.js·架构
扶苏10021 天前
Vue 3 响应式原理深度解析
前端·javascript·vue.js