当前位置: 首页 > news >正文

vue3中利用路由信息渲染菜单栏

1. 创建路由时将路由信息对象进行抽离

将路由信息对象单独抽离到router/routes.ts文件

关键:利用路由元信息meta,定义3个属性

  1. hidden:控制当前路由是否显示在菜单栏中
  2. title:菜单拦名称
  3. icon:对应菜单名称前面的图标
//对外暴露配置路由(常量路由)
export const constantRout = [{path: '/screen',component: () => import('@/views/screen/index.vue'),name: 'Screen',meta: {hidden: false,title: '数据大屏',icon: 'Platform',},},{path: '/acl',component: () => import('@/layout/index.vue'),name: 'Acl',meta: {hidden: false,title: '权限管理',icon: 'Lock',},children: [{path: '/acl/user',component: () => import('@/views/acl/user/index.vue'),name: 'User',meta: {hidden: false,title: '用户管理',icon: 'User',},},{path: '/acl/role',component: () => import('@/views/acl/role/index.vue'),name: 'Role',meta: {hidden: false,title: '角色管理',icon: 'UserFilled',},},{path: '/acl/permission',component: () => import('@/views/acl/permission/index.vue'),name: 'Permission',meta: {hidden: false,title: '菜单管理',icon: 'Monitor',},},],},
]

router/index.ts文件引入router.ts文件

import { createRouter, createWebHashHistory } from 'vue-router'
import { constantRoute } from './routes'
//创建路由器
const router = createRouter({//路由模式hashhistory: createWebHashHistory(),routes: constantRoute,//滚动行为scrollBehavior() {return {left: 0,top: 0,}},
})
export default router

2. 将路由信息对象挂载到pinia仓库中

将路由信息对象挂载到pinia仓库中,是为了数据共享,供组件使用

//引入路由(常量路由)
import { constantRoute } from '../../router/routes'
const useUserStore = defineStore('User', {state: (): userState => {return {token: GET_TOKEN(),// 存储路由信息用来遍历生成菜单结构menuRoutes: constantRoute, //仓库存储生成菜单需要数组(路由)}},
}

完成将路由信息挂载到pinia仓库中

3. 在组件中通过路由信息对象渲染菜单

Menu是封装的组件,接收父组件传递的userStore.menuRoutes也就是存放在pinia仓库中的路由信息对象

 <!-- 展示菜单区域 --><el-scrollbar class="scrllbar"><el-menu background-color="#001529" text-color="white"><!-- 根据路由动态生成菜单 --><Menu :menuList="userStore.menuRoutes"></Menu></el-menu></el-scrollbar>

在子组件Menu中声明接收:

//通过props接收父组件传递的路由组件信息
defineProps(['menuList'])

Menu组件完整写法:

思路:通过路由信息对象中的children属性判断是否有子路由,如果有子路由,则继续判断子路由的个数,从而进行对应的判断处理

<template><!-- 根据路由来遍历左侧菜单展示信息 --><template v-for="(item, index) in menuList" :key="item.path"><!-- 没有子路由 --><template v-if="!item.children"><el-menu-itemv-if="!item.meta.hidden":index="item.path"@click="goRoute"><template #title><el-icon><component :is="item.meta.icon"></component></el-icon><span>{{ item.meta.title }}</span></template></el-menu-item></template><!-- 有子路由但是只有一个 --><template v-else-if="item.children && item.children.length == 1"><el-menu-itemv-if="!item.children[0].meta.hidden":index="item.children[0].path"@click="goRoute"><template #title><el-icon><component :is="item.children[0].meta.icon"></component></el-icon><span>{{ item.children[0].meta.title }}</span></template></el-menu-item></template><!-- 有子路由且个数大于一个 --><template v-if="item.children && item.children.length > 1"><el-sub-menu :index="item.path"><template #title><el-icon><component :is="item.meta.icon"></component></el-icon><span>{{ item.meta.title }}</span></template><Menu :menuList="item.children"></Menu></el-sub-menu></template></template>
</template><script setup lang="ts">
import { useRouter } from 'vue-router'//通过props接收父组件传递的路由组件信息
defineProps(['menuList'])
let $router = useRouter()
const goRoute = (vc: any) => {//路由跳转$router.push(vc.index)
}
</script>
<script lang="ts">
// 当子路由个数大于等于一个时,并且或许子路由还有后代路由时。
// 这里我们使用了递归组件。递归组件需要命名(另外使用一个script标签,vue2格式)。
export default {name: 'Menu',
}
</script>
<style scoped lang="scss">
</style>

注意:在Menu组件中使用了Menu(递归组件),递归组件需要命名(另外使用一个script标签,vue2格式)。

export default {name: 'Menu',
}

注意:在el-menu-item组件中有一个click方法,对应一个函数,由element提供,会接收一个组件实例参数,并配合useRouter进行路由跳转

const goRoute = (vc: any) => {//路由跳转$router.push(vc.index)
}

http://www.lryc.cn/news/483949.html

相关文章:

  • Mysql每日一题(行程与用户,困难※)
  • adb 命令 查找启动的包名以及导出安装包
  • Flink_DataStreamAPI_输出算子Sink
  • 标准C++ 字符串
  • 时序预测:多头注意力+宽度学习
  • day06(单片机)IIC+STH20
  • Bugku CTF_Web——文件上传
  • C#版使用融合通信API发送手机短信息
  • 人工智能:重塑医疗、企业与生活的未来知识管理——以HelpLook为例
  • MVVM(Model-View-ViewModel)模型
  • 权限系统:权限应用服务设计
  • Android音频架构
  • AI 智享直播:开启直播新篇,引领未来互动新趋势!
  • 【AIGC】国内AI工具复现GPTs效果详解
  • Charles抓https包-配置系统证书(雷电)
  • 在卷积神经网络中真正占用内存的是什么
  • 2024 ECCV | DualDn: 通过可微ISP进行双域去噪
  • Elasticsearch 和 Kibana 8.16:Kibana 获得上下文和 BBQ 速度并节省开支!
  • Linux 抓包工具 --- tcpdump
  • Vector Optimization – Stride
  • git config是做什么的?
  • 计算机网络(7) 数据链路层
  • 2024年秋国开电大《建筑结构试验》形考任务1-4
  • 【MySQL】explain之type类型
  • Llama架构及代码详解
  • Android onConfigurationChanged 基础配置
  • 3. Sharding-Jdbc核⼼流 程+多种分⽚策略
  • 为什么财富的蓝图如此重要
  • 【云计算解决方案面试整理】1-2云计算基础概念及云计算技术原理
  • 循环语句 while()... 与 for()...(day11)