1、element-ui (用的vue2)
this.$refs.menuList.activeIndex
2、element-plus (用的vue3)
v-model 双向绑定,直接赋值
<template>
<el-menu :default-active="activeIndex" class="el-menu-demo" mode="horizontal" @select="handleSelect" ref="elMenus" v-model="activeIndex">
<div v-for="(item, index) in store.routes" :key="index">
<template v-if="!item.subMenu">
<el-menu-item :index="`${index}`" class="menu_title1">
<div class="menu_t">{{ item.title }}</div>
</el-menu-item>
</template>
<template v-else>
<el-sub-menu :index="`${index}`" class="menu_title1">
<template #title>
<div class="menu_t">{{ item.title }}</div>
</template>
<el-menu-item v-for="(subitem, subindex) in item.subMenu" :key="`${index}-${subindex}`"
:index="`${index}-${subindex}`">{{ subitem.title }}</el-menu-item>
</el-sub-menu>
</template>
</div>
</el-menu>
</template>
<script setup>
import { ref } from "vue";
import { useUserStore } from "@/stores";
const elMenus = ref(null)
const store = useUserStore();
const emits = defineEmits([
"toggleMenu",
]);
const activeIndex = ref("0");
activeIndex.value="1"//设置被选中的菜单
</script>