设置menuType来区分菜单类型

  /**
* @params menuType
* -1 一般为首页 / -> /home 只显示第一个子项
* -2 为无子菜单的菜单项 /config -> /config/person 无上下级,使用一级title
* -3 正常菜单 /system -> /system/1 | /system/2 有上下级
*/

先看看页面效果吧

  • type=1
  export default [
{
path: '/',
component: Layout,
redirect: '/home',
meta: { icon: 'House', menuType: 1 },
children: [
{
path: 'home',
component: () => import('@/views/home/index.vue'),
meta: { title: '首页' }
},
{
path: '401',
component: () => import('@/views/home/401.vue'),
meta: { title: '401', hideMenu: true }
},
{
path: '404',
component: () => import('@/views/home/404.vue'),
meta: { title: '404', hideMenu: true }
}
]
},
{
path: '/:pathMatch(.*)*',
redirect: '/404',
meta: { hideMenu: true }
}
]
  • type=2
  export default [
{
path: '/configuration',
component: Layout,
meta: { title: '配置管理', icon: 'MessageBox', menuType: 2 },
redirect: '/configuration/form',
children: [
{
path: 'form',
component: () => import('@/views/configuration/form/index.vue'),
meta: { title: '表单可视化', cache: false }
}
]
}
]
  • type=3
  export default [
{
path: '/dataset',
component: Layout,
meta: { title: '数据集管理', icon: 'DataAnalysis', menuType: 3 },
redirect: '/dataset/multi',
children: [
{
path: 'multi',
component: () => import('@/views/dataset/multi/index.vue'),
meta: { title: '共享数据集', cache: true }
},
{
path: 'person',
component: () => import('@/views/dataset/person/index.vue'),
meta: { title: '个人数据集', cache: true }
}
]
}
]

区别看得出来吧,看得出来吧,出来吧,吧!

最主要看看menu逻辑

menu:

  <template>
<el-scrollbar>
<el-menu
:default-active="activePath"
:collapse="!!collapseMenu"
:collapse-transition="false"
>
<menu-item
v-for="(menu, key) in allRoutes"
:key="key"
:menu="menu"
:path="menu.path"
/>
</el-menu>
</el-scrollbar>
</template> <script lang="ts" setup>
import { computed } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import MenuItem from './MenuItem.vue'
import { storeToRefs } from 'pinia'
import { SystemStore } from '@/store' const systemStore = SystemStore()
const { collapseMenu } = storeToRefs(systemStore) const route = useRoute()
const router = useRouter() const allRoutes = router.options.routes const activePath = computed(() => {
return route.path
})
</script> <style lang="scss" scoped>
.el-menu {
border: none;
}
</style>

menuItem:

  <template>
<template v-if="!props.menu.meta?.hideMenu">
<el-sub-menu v-if="props.menu.meta?.menuType === 3" :index="menuPath">
<template #title>
<el-icon>
<component :is="props.menu.meta?.icon"></component>
</el-icon>
<span>{{ props.menu.meta?.title }}</span>
</template>
<template v-for="children in props.menu.children" :key="children.path">
<menu-item
v-if="!children.meta?.hideMenu"
:menu="children"
:path="`${menuPath}/${children.path}`"
/>
</template>
</el-sub-menu>
<router-link v-else :to="menuPath">
<el-menu-item :index="menuPath">
<el-icon v-if="props.menu.meta?.icon">
<component :is="props.menu.meta.icon"></component>
</el-icon>
<template #title>
<span>{{
props.menu.meta?.menuType === 1
? props.menu.children[0].meta?.title
: props.menu.meta?.title
}}</span>
</template>
</el-menu-item>
</router-link>
</template>
</template> <script lang="ts" setup>
import { computed, PropType } from 'vue'
import { RouteRecordRaw } from 'vue-router'
const props = defineProps({
menu: {
type: Object as PropType<RouteRecordRaw>,
required: true
},
path: {
type: String,
default: ''
}
})
const menuPath = computed(() => {
if ([1, 2].includes(props.menu.meta?.menuType as number)) {
return (
(props.path === '/' ? props.path : props.path + '/') +
props.menu.children![0].path
)
}
return props.path
})
</script>

通过路由meta里面设置hideMenu属性来控制是否在菜单栏展示

menuType

  • 为1时(首页),取第一个children的信息
  • 为2时取主菜单信息,不展示子菜单
  • 为3时全部展示父子菜单

    然后就是获取path和取title的逻辑,看看代码就能懂

elementPlus配合vue-router搭建后台系统菜单模块的更多相关文章

  1. 保姆级别的vue + ElementUI 搭建后台管理系统教程

    vue + ElementUI 搭建后台管理系统记录 本文档记录了该系统从零配置的完整过程 项目源码请访问:https://gitee.com/szxio/vue2Admin,如果感觉对你有帮助,请点 ...

  2. 基于vue模块化开发后台系统——构建项目

    文章目录如下:项目效果预览地址项目开源代码基于vue模块化开发后台系统--准备工作基于vue模块化开发后台系统--构建项目基于vue模块化开发后台系统--权限控制 前言 在熟悉上一篇说到准备工具之后, ...

  3. react 高效高质量搭建后台系统 系列 —— 登录

    其他章节请看: react 高效高质量搭建后台系统 系列 登录 本篇将完成登录模块.效果和 spug 相同: 需求如下: 登录页的绘制 支持普通登录和LDAP登录 登录成功后跳转到主页,没有登录的情况 ...

  4. react 高效高质量搭建后台系统 系列 —— 系统布局

    其他章节请看: react 高效高质量搭建后台系统 系列 系统布局 前面我们用脚手架搭建了项目,并实现了登录模块,登录模块所依赖的请求数据和antd(ui框架和样式)也已完成. 本篇将完成系统布局.比 ...

  5. react 高效高质量搭建后台系统 系列 —— 表格的封装

    其他章节请看: react 高效高质量搭建后台系统 系列 表格 有一种页面在后台系统中比较常见:页面分上下两部分,上部分是 input.select.时间等查询项,下部分是查询项对应的表格数据.包含增 ...

  6. react 高效高质量搭建后台系统 系列 —— 前端权限

    其他章节请看: react 高效高质量搭建后台系统 系列 权限 本系列已近尾声,权限是后台系统必不可少的一部分,本篇首先分析spug项目中权限的实现,最后在将权限加入到我们的项目中来. spug 中权 ...

  7. 基于vue模块化开发后台系统——准备工作

    文章目录如下:项目效果预览地址项目开源代码基于vue模块化开发后台系统--准备工作基于vue模块化开发后台系统--构建项目基于vue模块化开发后台系统--权限控制 前言 本文章是以学习为主,练习一下v ...

  8. react 高效高质量搭建后台系统 系列 —— 脚手架搭建

    其他章节请看: react 高效高质量搭建后台系统 系列 脚手架搭建 本篇主要创建新项目 myspug,以及准备好环境(例如:安装 spug 中用到的包.本地开发和部署.自定义配置 react-app ...

  9. react 高效高质量搭建后台系统 系列 —— 请求数据

    其他章节请看: react 高效高质量搭建后台系统 系列 请求数据 后续要做登录模块(主页),需要先和后端约定JSON数据格式,将 axios 进行封装,实现本地的数据模拟 mockjs. Tip:s ...

  10. react 高效高质量搭建后台系统 系列 —— antd和样式

    其他章节请看: react 高效高质量搭建后台系统 系列 antd 后续要做登录模块(主页),不仅要解决请求数据的问题,还需要完成 antd 配置以及样式的准备. antd 多种主题风格 详情请看 这 ...

随机推荐

  1. es从线上库导出数据并导入开发环境

    背景 来了个需求,需要从某个线上es库查询一些数据出来并进行大屏展示.问需求方有没有开发环境的es库,答:没有,说要不直连他们的线上库. 后面想想也行吧,业务方都这么说了,结果开网络的流程被打回了,理 ...

  2. Python 解析JSON实现主机管理

    JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,它以易于阅读和编写的文本形式表示数据.JSON 是一种独立于编程语言的数据格式,因此在不同的编程语言中都有对 ...

  3. 8.1 Windows驱动开发:内核文件读写系列函数

    在应用层下的文件操作只需要调用微软应用层下的API函数及C库标准函数即可,而如果在内核中读写文件则应用层的API显然是无法被使用的,内核层需要使用内核专有API,某些应用层下的API只需要增加Zw开头 ...

  4. NetCat 工具的常用使用技巧

    netcat 黑客们的瑞士军刀,虽然小巧但是其功能一点也不弱,并且该工具天生免杀,值得你去尝试. NCwindows反弹 1:正向连接 服务器执行:nc -l -p 8888 -e cmd.exe 本 ...

  5. C/C++ 使用CRC检测磁盘文件完整性

    当软件被开发出来时,为了增加软件的安全性,防止被破解,通常情况下都会对自身内存或磁盘文件进行完整性检查,以防止解密者修改程序,我们可以将exe与dll文件同时做校验,来达到相互认证的目的,解密者想要破 ...

  6. LeetCode刷题日记 2020/03/26

    题干 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. 示例 1: 输入: "abcabcbb" 输出: 3 解释: 因为无重复字符的最长子串是 "abc ...

  7. Windows10安装Apache2.4.54并配置PHP5.6.40/PHP8.1.11

    环境 Windows 10 Apache2.4.54 PHP5.6.40/PHP8.1.11 安装Microsoft Visual C++ 下载地址:https://learn.microsoft.c ...

  8. ntp.conf详解

    linux系统的ntp.conf文件默认保存在/etc/ntp.conf 版本: [root@dsview ntpstats]# cat /etc/redhat-release CentOS rele ...

  9. 错误: tensorflow.python.framework.errors_impl.OutOfRangeError的解决方案

    近日,在使用CascadeRCNN完成目标检测任务时,我在使用这个模型训练自己的数据集时出现了如下错误: tensorflow.python.framework.errors_impl.OutOfRa ...

  10. PostgreSQL-可以通过localhost连接,无法通过IP地址连接。

    (1)如果PostgreSQL配置文件中没有允许访问该服务器的IP地址,则需要先添加允许访问的IP地址,并在防火墙中开放相应的端口.(2)在PostgreSQL配置文件postgresql.conf中 ...