element-plus menu 设置default-active 页面刷新时 导航高亮 踩坑

30 篇文章 0 订阅
5 篇文章 0 订阅

element-plus menu 设置default-active 页面刷新时 导航高亮

1、绑定default-active, index值为路由path (resourceUrl)
<el-menu
    :default-active="activeIndex"
    class="el-menu-demo"
    mode="horizontal"
    :router="true"
    @select="handleSelect"
  >
    <el-menu-item index="2dmap">首页</el-menu-item>
    <el-sub-menu :index="item.resourceUrl" v-for="item in menuResources" :key="item.resourceUrl">
      <template #title>{{item.resourceName}}</template>
      <el-menu-item v-for="route in item.menuChildren" :key='route.resourceUrl' :index="route.resourceUrl">{{route.resourceName}}</el-menu-item>
    </el-sub-menu>
    <el-menu-item></el-menu-item>
  </el-menu>
2、只需要在ref时把 $route.path绑定到响应数据源中,路由改变时切换高亮导航条,页面刷新时也会去获取 *自行动态改变activeIndex.value的值亲测无效
// setup
// 正确方式
const internalInstance = useCurrentInstance()
const activeIndex:Ref<string> = ref<string>(internalInstance.proxy.$route.path.split('/')[1])
// 错误方式
const activeIndex:Ref<string> = ref<string>('2dmap')
activeIndex.value = 'otherpath' //手动改变 无效
useCurrentInstance 方法
import { ComponentInternalInstance, getCurrentInstance } from 'vue'
export default function useCurrentInstance (): Record<string, any> {
  const { appContext } = getCurrentInstance() as ComponentInternalInstance
  const proxy = appContext.config.globalProperties
  return {
    proxy
  }
}
补充

使用useRouter获取路由信息

import { useRouter } from 'vue-router'
// setup
const { currentRoute } = useRouter()
const activeIndex:Ref<string> = ref<string>(currentRoute.value.path.split('/')[1])
要在Vue 3和Element Plus中实现三级菜单,你可以按照以下步骤进行配置: 1. 首先,在App.vue文件中配置顶栏,可以使用`<el-menu>`组件来创建多级菜单。在`<template>`标签中添加如下代码: ```html <div id="nav"> <div class="h-6" /> <el-menu default-active="/keng" class="el-menu-demo" mode="horizontal" background-color="#545c64" text-color="#fff" active-text-color="#ffd04b" router> <el-menu-item index="/dyg1">用户</el-menu-item> <el-menu-item index="/deg1">教学</el-menu-item> <el-menu-item index="/dsg1">题库</el-menu-item> </el-menu> <router-view/> </div> ``` 2. 其次,在需要设置侧边栏的页面中,可以使用`<el-menu>`组件来创建多级菜单。在`<template>`标签中添加如下代码: ```html <div class=''> <el-col :span="4"> <el-menu default-active="/yongh" class="el-menu-vertical-demo" background-color="#545c64" text-color="#fff" active-text-color="#ffd04b" router> <el-menu-item index="/dyg2"> <span>学员管理</span> </el-menu-item> <el-menu-item index="/dyg3"> <span>助教管理</span> </el-menu-item> </el-menu> </el-col> <el-col :span="20"> <router-view></router-view> </el-col> </div> ``` 3. 另外,如果你想动态渲染多级菜单,可以参考Vue Element-UI官方文档中提供的组件示例。 4. 最后,在router/index.js文件中设置路由,可以使用`const routes`来定义路由的路径和对应的组件。下面是一个示例代码: ```javascript const routes = [ { path: '/', redirect: '/dyg1', // 设置路由重定向第一次进入的页面 }, { path: '/dyg1', name: 'dyg1', component: () => import('../views/dyg/dyg1.vue'), children: [ { path: '/dyg2', name: 'dyg2', component: () => import('../views/dyg/dyg2.vue'), }, { path: '/dyg3', name: 'dyg3', component: () => import('../views/dyg/dyg3.vue'), }, ], }, ]; ``` 通过以上步骤,你可以在Vue 3和Element Plus中实现三级菜单的功能。<span class="em">1</span><span class="em">2</span><span class="em">3</span><span class="em">4</span>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值