news 2026/4/18 9:44:01

Element Plus Menu组件,实现点击目录而不是叶子节点也可以跳转,且支持高亮状态更新

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
Element Plus Menu组件,实现点击目录而不是叶子节点也可以跳转,且支持高亮状态更新
<template> <div class="m-nav-wrap"> <div class="m-nav"> <div class="m-logo"></div> <!-- 移动端:汉堡菜单按钮 --> <div v-if="isMobile" class="m-hamburger" @click="drawerVisible = true"> <span></span> <span></span> <span></span> </div> <!-- 桌面端:横向菜单 --> <el-menu v-if="!isMobile" :default-active="activeIndex" class="m-nav-menu" mode="horizontal" background-color="#001A33" text-color="#fff" active-text-color="#3489EB" @select="handleSelect" > <template v-for="menu in menuConfig" :key="menu.index"> <!-- 特殊按钮样式(联系我们) --> <div v-if="menu.isSpecialButton" class="m-special-button-wrapper"> <div class="m-contact-button" :class="{ 'is-active': activeIndex === menu.index }" @click="handleSelect(menu.index, [menu.index])" > {{ menu.label }} </div> </div> <!-- 一级菜单(无子菜单) --> <el-menu-item v-else-if="!menu.children" :index="menu.index"> {{ menu.label }} </el-menu-item> <!-- 一级菜单(有子菜单) --> <el-sub-menu v-else :index="menu.index" @click.native="handleSelectMenu(menu.index)"> <template #title>{{ menu.label }}</template> <!-- 二级菜单 --> <template v-for="child in menu.children" :key="child.index"> <!-- 二级菜单项(无子菜单) --> <el-menu-item v-if="!child.children" :index="child.index"> {{ child.label }} </el-menu-item> <!-- 二级菜单(有子菜单) --> <el-sub-menu v-else :index="child.index"> <template #title>{{ child.label }}</template> <el-menu-item v-for="grandChild in child.children" :key="grandChild.index" :index="grandChild.index" > {{ grandChild.label }} </el-menu-item> </el-sub-menu> </template> </el-sub-menu> </template> </el-menu> </div> <!-- 移动端:侧边抽屉菜单 --> <el-drawer v-model="drawerVisible" direction="ltr" :size="280" class="m-mobile-drawer" > <el-menu :default-active="activeIndex" class="m-mobile-menu" mode="vertical" background-color="#001A33" text-color="#fff" active-text-color="#3489EB" :router="true" @select="handleMobileSelect" > <template v-for="menu in menuConfig" :key="menu.index"> <!-- 一级菜单(无子菜单) --> <el-menu-item v-if="!menu.children" :index="menu.index"> {{ menu.label }} </el-menu-item> <!-- 一级菜单(有子菜单) --> <el-sub-menu v-else :index="menu.index" @click.native="handleSelectMenu(menu.index)"> <template #title>{{ menu.label }}</template> <!-- 二级菜单 --> <template v-for="child in menu.children" :key="child.index"> <!-- 二级菜单项(无子菜单) --> <el-menu-item v-if="!child.children" :index="child.index"> {{ child.label }} </el-menu-item> <!-- 二级菜单(有子菜单) --> <el-sub-menu v-else :index="child.index"> <template #title>{{ child.label }}</template> <el-menu-item v-for="grandChild in child.children" :key="grandChild.index" :index="grandChild.index" > {{ grandChild.label }} </el-menu-item> </el-sub-menu> </template> </el-sub-menu> </template> </el-menu> </el-drawer> </div> </template> <script lang="ts" setup> import { ref, onMounted, watch } from 'vue' import { useRouter, useRoute } from 'vue-router' import { menuConfig, setDocumentTitle } from './menu-config' import { useResponsive } from './use-responsive' const router = useRouter() const route = useRoute() const activeIndex = ref('/') const drawerVisible = ref(false) // 响应式检测 const { isMobile } = useResponsive() // 桌面端菜单选择 const handleSelect = (key: string, keyPath: string[]) => { console.log('Selected:', key, keyPath) // 更新激活状态 activeIndex.value = key // 设置页面标题 setDocumentTitle(key) // 路由跳转(仅当 index 是有效路径时) if (key.startsWith('/')) { router.push({ path: key }) } } // 移动端菜单选择 const handleMobileSelect = (key: string, keyPath: string[]) => { handleSelect(key, keyPath) // 关闭抽屉 drawerVisible.value = false } const handleSelectMenu = (path: any) => { console.log('path', path) // 更新激活状态 activeIndex.value = path // 设置页面标题 setDocumentTitle(path) // 路由跳转 router.push({ path }) } // 监听路由变化,同步更新激活状态 watch( () => route.path, (newPath) => { activeIndex.value = newPath setDocumentTitle(newPath) } ) onMounted(() => { // 初始化当前路由 const currentPath = router.currentRoute.value.path activeIndex.value = currentPath // 设置初始页面标题 setDocumentTitle(currentPath) }) </script> <style lang="css" scoped src="./index.css"></style>
// 菜单配置 export interface MenuItem { index: string label: string title?: string // 用于设置 document.title children?: MenuItem[] isSpecialButton?: boolean // 是否为特殊按钮样式 } export const menuConfig: MenuItem[] = [ { index: '/', label: '首页', }, { index: '/productCenter/index', label: '产品中心', children: [ { index: '/productCenter/index', label: '快速入口', }, { index: '/productCenter/aiAppOpPlat', label: 'AI应用运营平台', }, ], }, { index: '/successStories/index', label: '成功故事', title: '成功故事', children: [ { index: '/successStories/index', label: '快速入口', }, { index: '/successStories/edu', label: '教育', }, ], }, { index: '/partner', label: '合作伙伴', title: '合作伙伴', }, { index: '4', label: '关于我们', title: '关于我们', children: [ { index: '/about', label: '公司介绍', title: '公司介绍', }, ], }, { index: '6', label: '资讯中心', title: '资讯中心', children: [ { index: '/news', label: '最新资讯', title: '最新资讯', }, ], }, { index: '/contact', label: '联系我们', title: '联系我们', isSpecialButton: true, }, ] // 网站名称后缀 export const SITE_NAME = '通圆数智' // 根据 index 查找菜单标题 export function findMenuTitle( menuIndex: string, menus: MenuItem[] = menuConfig, ): string | null { for (const menu of menus) { if (menu.index === menuIndex) { return menu.title || menu.label } if (menu.children) { const title = findMenuTitle(menuIndex, menu.children) if (title) return title } } return null } // 设置页面标题 export function setDocumentTitle(menuIndex: string) { const title = findMenuTitle(menuIndex) if (title) { document.title = `${title} - ${SITE_NAME}` } }
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/4/17 15:19:42

必看!2026年十大溯源码好用的产品推荐榜单,引领安全追溯新风尚

在2026年&#xff0c;溯源码的应用日益广泛&#xff0c;成为产品质量管理的重要一环。本文将为您推荐十大优秀的溯源码产品&#xff0c;帮助您更好地实现产品追溯。这些工具不仅具备强大的功能&#xff0c;还能为各行业提供灵活的解决方案&#xff0c;确保产品安全与可靠性。每…

作者头像 李华
网站建设 2026/4/18 3:49:46

企微API突破:外部群的主动@全体成员实战

QiWe开放平台 个人名片 API驱动企微自动化&#xff0c;让开发更高效 核心能力&#xff1a;为开发者提供标准化接口、快速集成工具&#xff0c;助力产品高效拓展功能场景 官方站点&#xff1a;https://www.qiweapi.com 团队定位&#xff1a;专注企微API生态的技术服务团队 对接…

作者头像 李华
网站建设 2026/4/18 6:26:15

COMSOL模拟达西两相流在页岩气水平井压裂中的应力场耦合

comsol&#xff0c;达西两相流&#xff0c;页岩气水平井压裂&#xff0c;应力场耦合 页岩气开发这活儿&#xff0c;说简单点就是把石头缝里的气挤出来。水平井压裂技术就像给页岩层做"针灸"&#xff0c;但这次用的不是银针而是高压液体。COMSOL在这场景里简直像瑞士军…

作者头像 李华
网站建设 2026/4/17 19:09:51

企微中台架构:非官方接口与企业私有化 CRM 的深度集成

QiWe开放平台 个人名片 API驱动企微自动化&#xff0c;让开发更高效 核心能力&#xff1a;为开发者提供标准化接口、快速集成工具&#xff0c;助力产品高效拓展功能场景 官方站点&#xff1a;https://www.qiweapi.com 团队定位&#xff1a;专注企微API生态的技术服务团队 对接…

作者头像 李华
网站建设 2026/3/31 2:24:51

如何将照片从手机无线传输到笔记本电脑

了解如何将手机照片无线传输到笔记本电脑对于工作和个人生活都至关重要。无论是记录美好回忆还是处理重要的工作文档&#xff0c;无线传输照片都能节省时间和精力。本指南涵盖了五种无需数据线即可轻松传输照片的方法&#xff0c;从专用软件到云存储&#xff0c;每种方法都针对…

作者头像 李华
网站建设 2026/4/18 6:28:15

Chrome强开Gemini助手教程

Chrome强开Gemini助手教程 背景 Chrome中的Gemini助手是谷歌深度集成的AI浏览伴侣&#xff0c;核心能力覆盖内容理解、跨页整合、智能操作、创作辅助、自动浏览、图像编辑等重磅功能&#xff0c;具体如下&#xff1a;1.核心基础功能&#xff08;免费开放&#xff09;功能类别具…

作者头像 李华