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()
方法來獲取當(dāng)前注冊(cè)的所有路由信息。過濾和格式化數(shù)據(jù):
根據(jù)需要過濾和格式化路由數(shù)據(jù),以便它們可以被el-menu
組件使用。將數(shù)據(jù)傳遞給組件:
將格式化好的路由數(shù)據(jù)傳遞給使用el-menu
的組件。使用 v-for 渲染菜單:
在組件的模板中,使用v-for
指令來遍歷路由數(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>
來渲染對(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: '主頁', 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 庫實(shí)現(xiàn)的側(cè)邊導(dǎo)航菜單組件,它通過 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
:處理無子菜單項(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>)
- 定義了一些基本的樣式來美化菜單,例如移除邊框、設(shè)置字體大小和加粗。
總結(jié)
這個(gè)組件通過 Vue Router 的 useRouter
鉤子獲取當(dāng)前路由的配置信息,并根據(jù)這些信息動(dòng)態(tài)生成菜單項(xiàng)。使用 computed
屬性來區(qū)分哪些路由有子菜單,哪些沒有,然后相應(yīng)地渲染 el-menu-item
或 el-sub-menu
。點(diǎn)擊菜單項(xiàng)時(shí),使用 router.push
方法來改變頁面的路由,實(shí)現(xiàn)導(dǎo)航功能。并通過 useRoute
鉤子獲取當(dāng)前激活的路由,然后設(shè)置 activeMenu
來決定哪個(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實(shí)現(xiàn)ajax滾動(dòng)下拉加載,同時(shí)具有l(wèi)oading效果(推薦)
這篇文章主要介紹了vue實(shí)現(xiàn)ajax滾動(dòng)下拉加載,同時(shí)具有l(wèi)oading效果的實(shí)現(xiàn)代碼,文章包括難點(diǎn)說明,介紹的非常詳細(xì),感興趣的朋友參考下2017-01-01vue項(xiàng)目引入遠(yuǎn)程jweixin-1.2.0.js文件并使用詳解
這篇文章主要介紹了vue項(xiàng)目引入遠(yuǎn)程jweixin-1.2.0.js文件并使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2025-03-03vue watch監(jiān)聽變量值的實(shí)時(shí)變動(dòng)示例詳解
這篇文章主要介紹了vue 監(jiān)聽變量值的實(shí)時(shí)變動(dòng) watch,使用字符串形式的偵聽器函數(shù),還有一種是使用函數(shù)形式的偵聽器函數(shù),本文通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-08-08vue3使用Pinia修改state的三種方法(直接修改,$patch,actions)
Vue3?Pinia是一個(gè)狀態(tài)管理庫,專門為Vue3設(shè)計(jì)優(yōu)化,它提供了一種簡單而強(qiáng)大的方式來管理應(yīng)用程序的狀態(tài),并且與Vue3的響應(yīng)式系統(tǒng)緊密集成,本文給大家介紹了vue3使用Pinia修改state的三種方法,需要的朋友可以參考下2024-03-03