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绑定的就是动态的了。

相关推荐
YHHLAI21 分钟前
React `useState` 深入浅出
前端·javascript·react.js
斯内普吖35 分钟前
(开源)农产品电商系统实战指南 基于 Java + SpringBoot + Vue + MySQL
java·vue.js·spring boot·mysql·开源
半个落月39 分钟前
用 Harness 工程化缓解 LLM 幻觉:Best of N Sampling + LLM as Judge 实战
javascript·人工智能
三十而立洋43 分钟前
从 0 到 1:我用 VS Code API + Git 命令写了一个"代码统计"插件
javascript
daols881 小时前
vue 甘特图 vxe-gantt 紧前紧后依赖关系连接线配置详解
前端·vue.js·甘特图
云浪1 小时前
如何让大模型操作 MySQL 数据库?
javascript·人工智能·后端
lilian2332 小时前
Harmony os 技术实战|拼豆制图46:图片解码失败为何不能悄悄返回示例图
前端·javascript·华为·harmonyos
threerocks11 小时前
AI 原生软件开发生命周期手册 - 如何借助 AI,逐阶段改造软件开发生命周期
前端·javascript·后端
ly768912 小时前
JavaScript 从入门到进阶:核心语法、异步编程与工程化实践
开发语言·javascript·ecmascript
剑胆琴心静水深流13 小时前
全栈之路6---web集成与呈现
前端·vue.js·spring boot·分布式·spring·前端框架·npm