vue3+element-plus动态路由菜单示例代码
更新时间:2023年11月25日 15:18:01 作者:qq_39196447
这篇文章主要介绍了vue3+element-plus动态路由菜单示例代码,代码简单易懂,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
1、展示效果图

2、创建组件 SideMenu.vue
<!-- 侧边栏组件 -->
<template>
<div>
<el-menu :default-active="activeIndex" background-color="#001529" text-color="#ffffff">
<template v-for="(item, index) in menuList" :key="item.path">
<!-- 没有子路由 -->
<template v-if="!item.children">
<el-menu-item :index="item.path" v-if="!item.mate.hidden" @click="goPage">
<template #title>
<i class="iconfont" :class="item.mate.icon"></i>
<label>{{ item.mate.title }}</label>
</template>
</el-menu-item>
</template>
<!-- 只有一个子路由 -->
<template v-if="item.children && item.children.length == 1">
<el-menu-item v-if="!item.children[0].mate.hidden" :index="item.children[0].path" @click="goPage">
<template #title>
<i class="iconfont" :class="item.children[0].mate.icon"></i>
<label>{{ item.children[0].mate.title }}</label>
</template>
</el-menu-item>
</template>
<!-- 有两个及以上子路由 -->
<el-sub-menu v-if="item.children && item.children.length > 1" :index="item.path">
<template #title>
<i class="iconfont" :class="item.mate.icon"></i>
<label>{{ item.mate.title }}</label>
</template>
<!-- 递归组件 -->
<side-menu :menuList="item.children"></side-menu>
</el-sub-menu>
</template>
</el-menu>
</div>
</template>
<script setup lang="ts">
import { ref } from 'vue';
import { useRouter } from 'vue-router';
defineProps(['menuList']);
const activeIndex = ref('1');
const $router = useRouter();
// 路由跳转
const goPage = (vc) => {
$router.push(vc.index);
};
</script>
<script lang="ts">
// 当前组件的名称 用于递归组件使用
export default {
name: 'SideMenu',
};
</script>
<style scoped lang="scss">
.el-menu {
border-right: none;
.iconfont {
padding-right: 6px;
}
}
</style>3、路由示例
icon使用的是阿里巴巴图标库,需要下载在index.html全局引入
路由先存到store里面

创建routes.ts
export const constantRoute = [
{
path: '/login',
name: 'login',
component: () => import('@/views/login/login.vue'),
mate: {
title: '登录', //菜单标题
hidden: true, //是否隐藏:true隐藏,false不隐藏,默认hidden隐藏
},
},
{
path: '/',
name: 'layout',
redirect: '/home',
component: () => import('@/layout/index.vue'),
mate: {
title: 'layout', //菜单标题
hidden: true, //是否隐藏:true隐藏,false不隐藏
icon: '', //iconfont 名称
},
children: [
{
path: '/home',
name: 'home',
component: () => import('@/views/home/home.vue'),
mate: {
title: '首页', //菜单标题
icon: 'icon-shouye1', //iconfont 名称
},
},
],
},
{
path: '/screen',
name: 'screen',
component: () => import('@/views/screen/index.vue'),
mate: {
title: '数据大屏', //菜单标题
icon: 'icon-zonghefenxipingtai',
},
},
{
path: '/404',
name: '404',
component: () => import('@/views/404/index.vue'),
mate: {
title: '404', //菜单标题
hidden: true, //是否隐藏:true隐藏,false不隐藏
},
},
];4、在页面中使用
<el-scrollbar class="scroll-bar">
<side-menu :menuList="userStore.menuRoutes"></side-menu>
</el-scrollbar>
<script setup lang="ts">
import useUserStore from '@/store/modules/user';
let userStore = useUserStore();
</script>到此这篇关于vue3+element-plus动态路由菜单的文章就介绍到这了,更多相关vue3 element-plus路由菜单内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
Vue3 响应式 API 及 reactive 和 ref&
响应式是一种允许以声明式的方式去适应变化的编程范例,这篇文章主要介绍了关于Vue3响应式API及reactive和ref的用法,需要的朋友可以参考下2023-06-06
vue this.$refs.xxx报错undefined问题及解决
这篇文章主要介绍了vue this.$refs.xxx报错undefined问题及解决,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教2024-03-03
VuePress在build打包时window document is not defined问题解决
这篇文章主要为大家介绍了VuePress在build打包时window document is not defined问题解决,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪2023-07-07
Vue3+script setup+ts+Vite+Volar搭建项目
本文主要介绍了Vue3+script setup+ts+Vite+Volar搭建项目,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下2021-08-08


最新评论