news 2026/3/30 8:23:05

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/3/27 2:10:39

用AI写英文论文,又用降AIGC系统降AI率,这样做的意义在哪?

有同学直接用AI完成英文论文后发现论文AIGC率高&#xff0c;又用降AIGC系统把论文AI率降低&#xff0c;这么做的意义在哪&#xff1f; 自从AI问世以来&#xff0c;很多需要写论文的学生发现这类AI工具简直就是对于写作困难的人的大救星&#xff0c;利用AI工具可以完成各类论文…

作者头像 李华
网站建设 2026/3/27 9:16:07

凌晨两点还在调 Dify 环境?我后悔没早点知道 Sealos

那是一个普通的周三&#xff0c;产品经理下午三点甩来需求&#xff1a;"咱们搭个 AI 客服系统&#xff0c;用 Dify 对接 DeepSeek&#xff0c;下周演示。" 我心想&#xff1a;小场面。 然后时间快进到凌晨两点&#xff0c;我对着屏幕上第 47 次报错的 Docker Compose…

作者头像 李华
网站建设 2026/3/27 14:38:28

decode html

电子邮件 decode html 转义处理 支持 </& gt; 中间有空格的情况&#xff08;如 & lt;&#xff09;。这样即使内容被多次编码或含空格&#xff0c;也能正确恢复成 <img> 标签显示表情。 /*** 转义处理* 支持 </& gt; 中间有空格的情况&#xff08;如…

作者头像 李华
网站建设 2026/3/26 20:42:06

lvgl v8 label滚动模式

void lvgl_label_animation() {lv_obj_t* parent = lv_scr_act();lv_obj_t* lab = lv_label_create(

作者头像 李华
网站建设 2026/3/26 20:42:11

快速上手Ultimate#x2B;#x2B;的编译链接和配置

U简介 U&#xff08;全称 Ultimate&#xff09;是一个开源的 C 跨平台应用程序框架&#xff0c;以其高性能、低资源占用和高度集成的开发理念而闻名。它旨在提供“更少代码、更快执行”的开发体验。 主要特点&#xff1a; 高度集成 包含GUI、数据库、网络、XML、JSON等完整工…

作者头像 李华
网站建设 2026/3/28 9:09:32

Zigbee技术在智慧酒店中的应用设计与实现

Zigbee技术在智慧酒店中的应用设计与实现 一、应用背景与意义 在消费升级与数字化转型浪潮下&#xff0c;智慧酒店成为行业发展的核心方向&#xff0c;其核心需求是通过技术赋能提升宾客体验、优化运营效率、降低能耗成本。传统酒店控制系统多采用有线布线或单一无线技术&…

作者头像 李华