vue3+element-plus 實(shí)現(xiàn)動(dòng)態(tài)菜單和動(dòng)態(tài)路由的渲染的項(xiàng)目實(shí)踐
在 Vue.js 中,使用 Vue Router 管理路由數(shù)據(jù),并將其用于渲染 el-menu(Element UI 的菜單組件)通常涉及以下幾個(gè)步驟:
定義路由元數(shù)據(jù):
在你的路由配置中,為每個(gè)路由項(xiàng)添加meta字段,這個(gè)字段可以包含任何你想要傳遞給菜單的數(shù)據(jù),例如菜單名稱、圖標(biāo)等。獲取路由數(shù)據(jù):
使用router實(shí)例的getRoutes()方法來(lái)獲取當(dāng)前注冊(cè)的所有路由信息。過(guò)濾和格式化數(shù)據(jù):
根據(jù)需要過(guò)濾和格式化路由數(shù)據(jù),以便它們可以被el-menu組件使用。將數(shù)據(jù)傳遞給組件:
將格式化好的路由數(shù)據(jù)傳遞給使用el-menu的組件。使用 v-for 渲染菜單:
在組件的模板中,使用v-for指令來(lái)遍歷路由數(shù)據(jù),并渲染el-menu。
實(shí)現(xiàn)效果

路由配置
首先是路由配置,要實(shí)現(xiàn)這個(gè)路由配置,你需要完成以下幾個(gè)步驟:
注冊(cè) Vue Router:確保你已經(jīng)在你的 Vue 應(yīng)用中安裝并注冊(cè)了 Vue Router。
定義路由:使用 Vue Router 的
addRoute方法動(dòng)態(tài)添加路由。使用路由:在應(yīng)用中使用
<router-view>來(lái)渲染對(duì)應(yīng)的組件。渲染菜單:如果你需要根據(jù)路由配置動(dòng)態(tài)渲染菜單(例如使用 Element UI 的
el-menu),你需要從路由配置中提取必要的信息。
實(shí)現(xiàn)示例:
main.js
import { createApp } from 'vue';
import App from './App.vue';
import router from './router'; // 假設(shè)你有一個(gè) router 文件
const app = createApp(App);
app.use(router).mount('#app');
router.js
import { createRouter, createWebHistory } from 'vue-router';
import Main from '../views/Main.vue';
import Home from '../views/Home.vue';
import Goods from '../views/goods.vue';
import Order from '../views/order.vue';
const routes = [
{
path: '/',
name: 'main',
component: Main,
children: [
{
path: 'home',
name: 'home',
component: Home,
meta: { title: '主頁(yè)', icon: 'house' },
},
{
path: 'goods',
name: 'Goods',
component: Goods,
meta: { title: '商品列表', icon: 'el-icon-shopping-cart-full' },
},
{
path: 'order',
name: 'Order',
redirect: 'order/table',
children: [
{
path: 'table',
name: 'Table',
component: Order,
meta: { title: '訂單列表', icon: 'el-icon-s-fold' },
},
],
meta: { title: '訂單', icon: 'el-icon-s-claim' },
},
],
},
];
const router = createRouter({
history: createWebHistory(process.env.BASE_URL),
routes,
});
export default router;
App.vue
<template>
<div id="app">
<router-view></router-view>
</div>
</template>
<script>
export default {
name: 'App',
};
</script>
動(dòng)態(tài)渲染菜單
如果你想根據(jù)路由配置動(dòng)態(tài)渲染菜單(el-menu),你可以在組件中使用 Vue Router 的 routes 屬性。以下是一個(gè)使用組合式 API 的示例:
MenuComponent.vue
<template>
<el-aside :width="width">
<el-menu
background-color="#fff"
active-text-color="#409eff"
class="aside-menu"
:collapse="isCollapse"
:default-active="activeMenu"
>
<el-menu-item
v-for="item in noChilden"
:index="item.path"
:key="item.path"
@click="handlemenu(item)"
>
<component class="icon" :is="item.meta.icon"></component>
<span>{{ item.meta.title }}</span>
</el-menu-item>
<el-sub-menu
v-for="item in hasChilden"
:index="item.path"
:key="item.path"
>
<template #title>
<component class="icon" :is="item.meta.icon"></component>
<span>{{ item.meta.title }}</span>
</template>
<!-- <el-menu-item-group> -->
<el-menu-item
v-for="subItem in item.children"
:key="subItem.path"
:index="subItem.path"
@click="handlemenuchild(item,subItem)"
class="sub-menu-deep"
>
<component class="icon" :is="subItem.meta.icon"></component>
<span>{{ subItem.meta.title }}</span>
</el-menu-item>
<!-- </el-menu-item-group> -->
</el-sub-menu>
</el-menu>
</el-aside>
</template>
<script setup>
import {ref,computed,onMounted} from 'vue'
import {useAllDataStore} from "@/store"
import {useRouter,useRoute} from 'vue-router'
const list = ref([]);
const router = useRouter();
onMounted(() =>{
//找到根路由
const childRoutes = router.getRoutes().find(route => route.path === '/').children;
list.value = childRoutes.filter(route => route.meta && route.meta.title);
console.log(list.value);
});
const hasChilden = computed(() => list.value.filter(item => item.children && item.children.length > 0))
const noChilden = computed(() => list.value.filter(item => !item.children || item.children.length === 0))
const route = useRoute()
const handlemenu = (item) =>{
router.push(item.path)
}
const handlemenuchild = (item,subItem) =>{
router.push(item.path+'/'+ subItem.path)
}
const activeMenu = computed(() => route.path)
</script>
<style lang="less" scoped>
.icon{
width:18px;
height: 18px;
margin-right: 5px;
}
.el-menu {
border-right: none;
font-size: 20px;
font-weight: bold;
h3{
line-height: 48px;
text-align: center;
}
}
.el-aside{
height:100%;
background-color: #fff;
// box-shadow: 1px 0px 5px rgba(0, 0, 0, 0.2);
border-right: 1px solid #e4e7ed;
}
</style>解釋上面是怎么實(shí)現(xiàn)router 動(dòng)態(tài)渲染菜單的
這段代碼是一個(gè)使用 Vue 3 和 Element UI 庫(kù)實(shí)現(xiàn)的側(cè)邊導(dǎo)航菜單組件,它通過(guò) Vue Router 的路由信息動(dòng)態(tài)渲染菜單項(xiàng)。下面是代碼的詳細(xì)解釋:
布局中引入組件
<script setup>
import CommonAside from '@/components/.vue'
import CommonHeader from '../components/MenuComponent.vue';
import CommonTab from '@/components/CommonTab.vue'
</script>
<template>
<div class="common-layput">
<el-container class="lay-container">
<!-- 自定義左側(cè)菜單組件 -->
<menu-component />
<el-container>
<el-header class="el-header">
<common-header />
</el-header>
<el-main class="right-mian">
<!-- 路由顯示內(nèi)容 -->
<router-view></router-view>
</el-main>
</el-container>
</el-container>
</div>
</template>
<style scoped lang="less">
.common-layput,.lay-container{
height:100%;
}
.el-header{
background-color: #fff;
box-shadow: 1px 0px 5px rgba(0, 0, 0, 0.2);
}
</style>
模板部分 (<template>)
- 使用
el-aside組件作為側(cè)邊欄容器。 el-menu組件用于創(chuàng)建菜單,其中:background-color和active-text-color設(shè)置菜單的背景色和激活項(xiàng)的文字顏色。class添加自定義類名。:collapse屬性用于控制菜單的展開和收起狀態(tài)。:default-active綁定當(dāng)前激活的菜單項(xiàng)路徑。
菜單項(xiàng)和子菜單項(xiàng)渲染
- 使用
el-menu-item組件渲染沒有子菜單的頂級(jí)菜單項(xiàng)。 - 使用
el-sub-menu組件渲染帶有子菜單的菜單項(xiàng)。
腳本部分 (<script setup>)
- 引入必要的 Vue 組合式 API 鉤子:
ref,computed,onMounted,useRouter,useRoute。 list引用用于存儲(chǔ)從 Vue Router 獲取的路由信息。
路由信息獲取
- 在
onMounted鉤子中,使用useRouter鉤子的getRoutes方法找到根路由'/'的子路由,并篩選出包含meta和meta.title的路由,存儲(chǔ)到list.value。
計(jì)算屬性
hasChilden:計(jì)算屬性,篩選出list.value中具有子菜單的路由。noChilden:計(jì)算屬性,篩選出list.value中沒有子菜單的路由。
導(dǎo)航處理函數(shù)
handlemenu:處理無(wú)子菜單項(xiàng)的點(diǎn)擊事件,使用router.push方法導(dǎo)航到點(diǎn)擊的菜單項(xiàng)路徑。handlemenuchild:處理有子菜單項(xiàng)的點(diǎn)擊事件,拼接父菜單和子菜單的路徑,然后導(dǎo)航。
激活狀態(tài)
activeMenu:計(jì)算屬性,根據(jù)當(dāng)前路由的路徑route.path設(shè)置激活的菜單項(xiàng)。
樣式部分 (<style>)
- 定義了一些基本的樣式來(lái)美化菜單,例如移除邊框、設(shè)置字體大小和加粗。
總結(jié)
這個(gè)組件通過(guò) Vue Router 的 useRouter 鉤子獲取當(dāng)前路由的配置信息,并根據(jù)這些信息動(dòng)態(tài)生成菜單項(xiàng)。使用 computed 屬性來(lái)區(qū)分哪些路由有子菜單,哪些沒有,然后相應(yīng)地渲染 el-menu-item 或 el-sub-menu。點(diǎn)擊菜單項(xiàng)時(shí),使用 router.push 方法來(lái)改變頁(yè)面的路由,實(shí)現(xiàn)導(dǎo)航功能。并通過(guò) useRoute 鉤子獲取當(dāng)前激活的路由,然后設(shè)置 activeMenu 來(lái)決定哪個(gè)菜單項(xiàng)應(yīng)該處于激活狀態(tài)。這個(gè)組件是一個(gè)結(jié)合 Vue Router 和 Element UI 的動(dòng)態(tài)菜單實(shí)現(xiàn),它可以自動(dòng)根據(jù)路由配置渲染出相應(yīng)的菜單結(jié)構(gòu),并且能夠響應(yīng)路由變化,高亮顯示當(dāng)前激活的菜單項(xiàng)。
到此這篇關(guān)于vue3+element-plus 實(shí)現(xiàn)動(dòng)態(tài)菜單和動(dòng)態(tài)路由的渲染的項(xiàng)目實(shí)踐的文章就介紹到這了,更多相關(guān)vue3+element-plus 動(dòng)態(tài)菜單和動(dòng)態(tài)路由內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue表單組件進(jìn)階之如何打造屬于你的自定義v-model
在Vue中自定義組件是一個(gè)強(qiáng)大的功能,它允許你將可重用的代碼封裝成獨(dú)立的模塊,從而提高開發(fā)效率和代碼的可維護(hù)性,這篇文章主要介紹了Vue表單組件進(jìn)階之如何打造屬于你的自定義v-model的相關(guān)資料,需要的朋友可以參考下2025-11-11
vue的狀態(tài)庫(kù)管理實(shí)現(xiàn)示例
Vuex 是 Vue.js 官方推薦的狀態(tài)管理庫(kù)之一,本文主要介紹了vue的狀態(tài)庫(kù)管理實(shí)現(xiàn)示例,具有一定的參考價(jià)值,感興趣的可以了解一下2024-04-04
Vue實(shí)現(xiàn)PC端分辨率自適應(yīng)的示例代碼
本文主要介紹了Vue實(shí)現(xiàn)PC端分辨率自適應(yīng)的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08
vue3使用ref和reactive定義數(shù)據(jù)詳解
文章主要介紹了在Vue 3中使用`ref`和`reactive`來(lái)定義和操作數(shù)據(jù)的區(qū)別,`ref`適用于基本數(shù)據(jù)類型和部分引用數(shù)據(jù)類型,操作時(shí)需要通過(guò)`.value`訪問(wèn)和修改,`reactive`主要用于對(duì)象類型的引用數(shù)據(jù),操作時(shí)可以直接訪問(wèn)和修改屬性,但在模板中使用時(shí)需要通過(guò)定義時(shí)的變量來(lái)訪問(wèn)2025-10-10
vue 3.x 中mixin封裝公用方法應(yīng)用方式
這篇文章主要介紹了vue 3.x 中mixin封裝公用方法應(yīng)用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-05-05

