vue3 el-menu初始化时选中没有高亮的问题(default-active和index的问题)

首先看官方文档的示例:

需要注意的是:

1、default-active的值是字符串,那么index绑定的值也要是字符串,且数字对应。不能default-avtive绑定的是1,而menu-item的index绑定的是45

2、default-active的值是当前选中menu-item项的index

所以el-menu-item的index项一定要和default-active对应起来。

javascript 复制代码
<el-menu
    :default-active="activeIndex"
    class="el-menu-demo"
    mode="horizontal"
    @select="handleSelect"
  >
    <el-menu-item index="1">Processing Center</el-menu-item>
    <el-sub-menu index="2">
      <template #title>Workspace</template>
      <el-menu-item index="2-1">item one</el-menu-item>
      <el-menu-item index="2-2">item two</el-menu-item>
      <el-menu-item index="2-3">item three</el-menu-item>
      <el-sub-menu index="2-4">
        <template #title>item four</template>
        <el-menu-item index="2-4-1">item one</el-menu-item>
        <el-menu-item index="2-4-2">item two</el-menu-item>
        <el-menu-item index="2-4-3">item three</el-menu-item>
      </el-sub-menu>
    </el-sub-menu>
    <el-menu-item index="3" disabled>Info</el-menu-item>
    <el-menu-item index="4">Orders</el-menu-item>
  </el-menu>
<script lang="ts" setup>
import { ref } from 'vue'
const activeIndex = ref('1')
const handleTest = (key,keyPath) => {
  console.log('打印输出menuSelect事件的参数---',activeIndex.value,key,keyPath)
}
</script>

再次点击其他项

为了避免页面刷新时menu组件的选中样式消失,所以在每次点击的时候我们要将default-active重新赋值。这样:default-active绑定的就是动态的了。

相关推荐
云浪8 小时前
Go 并发实战:从零实现一个高并发安全的任务调度器
javascript·后端·go
OpsEye16 小时前
大模型账单对账难:不同厂商 Token 统计口径差异怎么处理
javascript·ai编程
全栈项目管理程序猿16 小时前
ArcGIS JS 基础教程(32):体元系列 - VoxelSlice 体元切片
前端·javascript
流烟默18 小时前
SSE 实战:在 SpringBoot + Vue 项目中零依赖实现轻量级实时消息推送
vue.js·spring boot·后端·sse
Hilaku18 小时前
AI 流式输出都用 SSE,什么时候必须换 WebSocket?
前端·javascript·程序员
奇牙coding19 小时前
Codex CLI 升级后多文件编辑静默失效:config.yaml 枚举变更排查与修复指南
java·前端·javascript·ai
执子念的飞鱼19 小时前
纯前端预览 275 种文件格式是怎么做到的
前端·vue.js·react.js
不简说19 小时前
AI 帮你画模板、点一下就云打印:sv-print 这三个月都干了啥
前端·javascript·人工智能
大明二代20 小时前
从零构建 Mastra RAG MVP:DeepSeek、SiliconFlow 与 PostgreSQL/pgvector 实战
javascript·数据库·人工智能
结局无敌20 小时前
React Native for OpenHarmony 实战:三方库 react-native-paper 的鸿蒙化适配指南
javascript·人工智能·深度学习·react native·react.js·ecmascript·harmonyos