点击侧边栏菜单时只切换 <router-view> 中的内容,而不是进行整个页面的路由跳转(动态路由)

解决方法:在 <el-menu> @select 事件中调用了 handleMenuSelect 方法来处理菜单项的选择。你可以在 handleMenuSelect 方法中根据菜单项的 index 来执行相应的操作,例如更新组件内的数据或者切换组件。由于整个页面的路由路径并没有改变,因此不会触发整个页面的路由跳转,只会更新 <router-view> 中的内容。这样就实现了只更新 <router-view> 中内容的效果。 

home组件

<template>
  <div class="container">
    <el-container>
      <!-- 头部 -->
      <el-header>Header</el-header>
      <el-container>
        <!-- 侧边栏 -->
        <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="personal">
              <i class="el-icon-document"></i>
              <span slot="title">成绩管理</span>
            </el-menu-item>
            <el-menu-item index="score">
              <i class="el-icon-setting"></i>
              <span slot="title">人员管理</span>
            </el-menu-item>
          </el-menu>
        </el-col>
        <!-- 主要内容 -->
        <el-main>
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>
export default {
  methods: {
    handleMenuSelect(index) {
      const targetPath = '/' + index;
      
      // 判断目标路径是否与当前路径相同
      // 通过 this.$route.path 获取到当前路由的路径
      if (this.$route.path === targetPath) {
        // 如果相同则不进行导航
        return;
      }
      
      // 否则进行导航
      this.$router.push({ path: targetPath });
    }
  }
};
</script>


<style scoped>
.container {

  width: 1200px;
  margin: 0 auto;
}

.el-header {
  background-color: #B3C0D1;
  color: #333;
  text-align: center;
  line-height: 60px;
}

.el-aside {

  text-align: center;

}

.el-main {
  height: 600px;
  background-color: #E9EEF3;
 
}

body>.el-container {
  margin-bottom: 40px;
}
</style>

路由:

import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)
import home from '@/view/home'  //引入需要用的组件
import login from '@/view/login'  //引入需要用的组件
import first from '@/view/aside/first'
import person from '@/view/aside/person'
import personal from '@/view/aside/personal'
import score from '@/view/aside/score'


const routes = [
	{
		path: '/',
		redirect: '/home' // 将根路径重定向到 home 路由
	},

	{
		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: '/login',
		name: 'login',
		component: login
	}
	
]

const router = new VueRouter({
	mode: 'history',
	base: process.env.BASE_URL,
	routes
});


export default router

目录

最近更新

  1. TCP协议是安全的吗?

    2024-02-15 17:24:01       19 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-02-15 17:24:01       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-02-15 17:24:01       20 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-02-15 17:24:01       20 阅读

热门阅读

  1. hpp文件:C++开发中的利器

    2024-02-15 17:24:01       28 阅读
  2. 【zabbix】(四)-钉钉告警&企业微信配置

    2024-02-15 17:24:01       52 阅读
  3. Rust的if let语法:更简洁的模式匹配

    2024-02-15 17:24:01       28 阅读
  4. 【ASP.NET 6 Web Api 全栈开发实战】--前言

    2024-02-15 17:24:01       28 阅读
  5. 作业2024/2/15

    2024-02-15 17:24:01       27 阅读
  6. D. Yet Another Sorting Problem - 树状数组求逆序数

    2024-02-15 17:24:01       28 阅读
  7. AGV-产品设计概述

    2024-02-15 17:24:01       33 阅读
  8. 聚集索引选取规则

    2024-02-15 17:24:01       31 阅读