elementPlus配合vue-router搭建后台系统菜单模块
设置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搭建后台系统菜单模块的更多相关文章
- 保姆级别的vue + ElementUI 搭建后台管理系统教程
vue + ElementUI 搭建后台管理系统记录 本文档记录了该系统从零配置的完整过程 项目源码请访问:https://gitee.com/szxio/vue2Admin,如果感觉对你有帮助,请点 ...
- 基于vue模块化开发后台系统——构建项目
文章目录如下:项目效果预览地址项目开源代码基于vue模块化开发后台系统--准备工作基于vue模块化开发后台系统--构建项目基于vue模块化开发后台系统--权限控制 前言 在熟悉上一篇说到准备工具之后, ...
- react 高效高质量搭建后台系统 系列 —— 登录
其他章节请看: react 高效高质量搭建后台系统 系列 登录 本篇将完成登录模块.效果和 spug 相同: 需求如下: 登录页的绘制 支持普通登录和LDAP登录 登录成功后跳转到主页,没有登录的情况 ...
- react 高效高质量搭建后台系统 系列 —— 系统布局
其他章节请看: react 高效高质量搭建后台系统 系列 系统布局 前面我们用脚手架搭建了项目,并实现了登录模块,登录模块所依赖的请求数据和antd(ui框架和样式)也已完成. 本篇将完成系统布局.比 ...
- react 高效高质量搭建后台系统 系列 —— 表格的封装
其他章节请看: react 高效高质量搭建后台系统 系列 表格 有一种页面在后台系统中比较常见:页面分上下两部分,上部分是 input.select.时间等查询项,下部分是查询项对应的表格数据.包含增 ...
- react 高效高质量搭建后台系统 系列 —— 前端权限
其他章节请看: react 高效高质量搭建后台系统 系列 权限 本系列已近尾声,权限是后台系统必不可少的一部分,本篇首先分析spug项目中权限的实现,最后在将权限加入到我们的项目中来. spug 中权 ...
- 基于vue模块化开发后台系统——准备工作
文章目录如下:项目效果预览地址项目开源代码基于vue模块化开发后台系统--准备工作基于vue模块化开发后台系统--构建项目基于vue模块化开发后台系统--权限控制 前言 本文章是以学习为主,练习一下v ...
- react 高效高质量搭建后台系统 系列 —— 脚手架搭建
其他章节请看: react 高效高质量搭建后台系统 系列 脚手架搭建 本篇主要创建新项目 myspug,以及准备好环境(例如:安装 spug 中用到的包.本地开发和部署.自定义配置 react-app ...
- react 高效高质量搭建后台系统 系列 —— 请求数据
其他章节请看: react 高效高质量搭建后台系统 系列 请求数据 后续要做登录模块(主页),需要先和后端约定JSON数据格式,将 axios 进行封装,实现本地的数据模拟 mockjs. Tip:s ...
- react 高效高质量搭建后台系统 系列 —— antd和样式
其他章节请看: react 高效高质量搭建后台系统 系列 antd 后续要做登录模块(主页),不仅要解决请求数据的问题,还需要完成 antd 配置以及样式的准备. antd 多种主题风格 详情请看 这 ...
随机推荐
- CouchDB vs. LevelDB
CouchDB 和 LevelDB 都是数据库系统,但它们在很多方面有着不同的设计和应用重点.下面是对这两个数据库在一些关键点上的对比: 数据模型: CouchDB:CouchDB 是一种面向文档的数 ...
- c++基础之语句
上一次总结了一下c++中表达式的相关内容,这篇博文主要总结语句的基础内容 简单语句 c++ 中语句主要是以分号作为结束符的,最简单的语句是一个空语句,空语句主要用于,语法上需要某个地方,但是逻辑上不需 ...
- Elasticsearch实战:常见错误及详细解决方案
Elasticsearch实战:常见错误及详细解决方案 1.read_only_allow_delete":"true" 当我们在向某个索引添加一条数据的时候,可能(极少 ...
- 知识图谱项目实战(一):瑞金医院MMC人工智能辅助构建知识图谱--初赛实体识别【1】
1.技术背景&赛题介绍: A Labeled Chinese Dataset for Diabetes中文糖尿病标注数据集详情请见. 数据集链接:瑞金医院MMC人工智能辅助构建知识数据源:知识 ...
- docker 安装的jenkins如何执行宿主机的shell启动jar包
最近在用docker做自动化部署的时候遇到一个问题,就是用docker装的jenkins可以通过映射执行宿主机脚本.但是,却无法通过shell脚本启动宿主机jar包.经排查最终用以下方案完美解决. 一 ...
- Python内置小工具(非常实用!)
一.1秒钟启动一个下载服务器在工作中时不时会有这样的一个需求:将服务器(或者自己电脑)上的文件传给其他同事.将文件传给同事本身并不是一个很繁琐的工作,现在的聊天工具一般都支持文件传输.但是,如果需要传 ...
- JS Leetcode 81. 搜索旋转排序数组 II 题解,补救二分法的可行性
壹 ❀ 引 今日LeetCode题为153. 寻找旋转排序数组中的最小值,在10个月前,我已在JS leetcode 寻找旋转排序数组中的最小值 题解分析,你不得不了解的二分法一文中写了本题的题解,所 ...
- ES6学习 第六章 数值的扩展
前言 本章介绍数值的扩展.新增了很多方法,有些不常用的方法了解即可. 本章原文链接:数值的扩展 进制表示法 ES6 提供了二进制和八进制数值的新的写法,分别用前缀0b(或0B)和0o(或0O)表示. ...
- Educational Codeforces Round 136 (Rated for Div. 2) A-E
比赛链接 A 题解 知识点:模拟. 所有点都跑一遍即可. 另外可以不模拟, \(\geq 2*2\) 的情况都可以摆在 \((2,2)\) 这个点,其他摆在 \((1,1)\) . 时间复杂度 \(O ...
- Moon.Orm版本维护及下载
MoonOrm最新版及代码生成器 (2020-8-29)