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

相关推荐
Dxy12393102169 小时前
JS DES 加密详解
开发语言·javascript·arcgis
充值内卷9 小时前
我给桌面加了个鱼缸:C++ 写后端,WebView2 里养一座 Three.js 小岛
开发语言·javascript·c++
椒哥11 小时前
一天被问几十次"允许",我统计了 517 次,然后写了个插件
前端·javascript·github
kyriewen12 小时前
我测了 6 种防请求竞态的写法:只有 1 种真能打
前端·javascript·程序员
OpsEye13 小时前
企业大模型压测,需要重点关注哪些指标?
javascript·ai编程
LuHang13 小时前
从天地图切换到高德地图:一次真实的底图迁移实战
前端·vue.js·cesium
像我这样帅的人丶你还13 小时前
我html生成的doc文档为何在手机文档显示不了图片?
前端·javascript
ruanno_top17 小时前
Vue 3 后台权限路由:我漏掉了按钮级裁剪,还把 403 写成了 404
vue.js
栉甜17 小时前
Vue2学习内容(上)
前端·javascript·vue.js·学习
shmily麻瓜小菜鸡19 小时前
TypeScript 知识点补充(第三部分:工程化与高级类型)
前端·javascript·typescript