vue3+element-plus動(dòng)態(tài)路由菜單示例代碼
1、展示效果圖

2、創(chuàng)建組件 SideMenu.vue
<!-- 側(cè)邊欄組件 -->
<template>
<div>
<el-menu :default-active="activeIndex" background-color="#001529" text-color="#ffffff">
<template v-for="(item, index) in menuList" :key="item.path">
<!-- 沒(méi)有子路由 -->
<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>
<!-- 只有一個(gè)子路由 -->
<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>
<!-- 有兩個(gè)及以上子路由 -->
<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();
// 路由跳轉(zhuǎn)
const goPage = (vc) => {
$router.push(vc.index);
};
</script>
<script lang="ts">
// 當(dāng)前組件的名稱 用于遞歸組件使用
export default {
name: 'SideMenu',
};
</script>
<style scoped lang="scss">
.el-menu {
border-right: none;
.iconfont {
padding-right: 6px;
}
}
</style>3、路由示例
icon使用的是阿里巴巴圖標(biāo)庫(kù),需要下載在index.html全局引入
路由先存到store里面

創(chuàng)建routes.ts
export const constantRoute = [
{
path: '/login',
name: 'login',
component: () => import('@/views/login/login.vue'),
mate: {
title: '登錄', //菜單標(biāo)題
hidden: true, //是否隱藏:true隱藏,false不隱藏,默認(rèn)hidden隱藏
},
},
{
path: '/',
name: 'layout',
redirect: '/home',
component: () => import('@/layout/index.vue'),
mate: {
title: 'layout', //菜單標(biāo)題
hidden: true, //是否隱藏:true隱藏,false不隱藏
icon: '', //iconfont 名稱
},
children: [
{
path: '/home',
name: 'home',
component: () => import('@/views/home/home.vue'),
mate: {
title: '首頁(yè)', //菜單標(biāo)題
icon: 'icon-shouye1', //iconfont 名稱
},
},
],
},
{
path: '/screen',
name: 'screen',
component: () => import('@/views/screen/index.vue'),
mate: {
title: '數(shù)據(jù)大屏', //菜單標(biāo)題
icon: 'icon-zonghefenxipingtai',
},
},
{
path: '/404',
name: '404',
component: () => import('@/views/404/index.vue'),
mate: {
title: '404', //菜單標(biāo)題
hidden: true, //是否隱藏:true隱藏,false不隱藏
},
},
];4、在頁(yè)面中使用
<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>到此這篇關(guān)于vue3+element-plus動(dòng)態(tài)路由菜單的文章就介紹到這了,更多相關(guān)vue3 element-plus路由菜單內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue3 響應(yīng)式 API 及 reactive 和 ref&
響應(yīng)式是一種允許以聲明式的方式去適應(yīng)變化的編程范例,這篇文章主要介紹了關(guān)于Vue3響應(yīng)式API及reactive和ref的用法,需要的朋友可以參考下2023-06-06
解決nuxt 自定義全局方法,全局屬性,全局變量的問(wèn)題
這篇文章主要介紹了解決nuxt 自定義全局方法,全局屬性,全局變量的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-11-11
vue?this.$refs.xxx報(bào)錯(cuò)undefined問(wèn)題及解決
這篇文章主要介紹了vue?this.$refs.xxx報(bào)錯(cuò)undefined問(wèn)題及解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03
web項(xiàng)目開(kāi)發(fā)中2個(gè)Token原因解析及示例代碼
這篇文章主要介紹了web項(xiàng)目開(kāi)發(fā)中會(huì)出現(xiàn)2個(gè)Token原因的解析以及實(shí)現(xiàn)的示例代碼,有需要的同學(xué)可以借鑒參考下,希望可以有所幫助2021-09-09
Vue實(shí)現(xiàn)input寬度隨文字長(zhǎng)度自適應(yīng)操作
這篇文章主要介紹了Vue實(shí)現(xiàn)input寬度隨文字長(zhǎng)度自適應(yīng)操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-07-07
VuePress在build打包時(shí)window?document?is?not?defined問(wèn)題解決
這篇文章主要為大家介紹了VuePress在build打包時(shí)window?document?is?not?defined問(wèn)題解決,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07
vue-element-admin如何設(shè)置默認(rèn)語(yǔ)言
這篇文章主要介紹了vue-element-admin如何設(shè)置默認(rèn)語(yǔ)言,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04
Vue-router的使用和出現(xiàn)空白頁(yè),路由對(duì)象屬性詳解
今天小編就為大家分享一篇Vue-router的使用和出現(xiàn)空白頁(yè),路由對(duì)象屬性詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-09-09
Vue3+script setup+ts+Vite+Volar搭建項(xiàng)目
本文主要介紹了Vue3+script setup+ts+Vite+Volar搭建項(xiàng)目,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08
vue如何設(shè)置定時(shí)器和清理定時(shí)器
這篇文章主要介紹了vue如何設(shè)置定時(shí)器和清理定時(shí)器,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-05-05

