Vue3 根據路由動態(tài)生成側邊菜單的方法
在 Vue3 的項目開發(fā),尤其是后臺管理系統(tǒng)這類復雜應用場景中,側邊菜單扮演著舉足輕重的角色,它是用戶快速導航至各個功能模塊的得力助手。而根據路由動態(tài)生成側邊菜單,則為系統(tǒng)的靈活性和可擴展性增添了強大動力。接下來,我們將深入探討如何在 Vue3 中實現這一關鍵功能。
gitCode代碼地址:https://gitcode.com/Jiaberrr/vue3-pc-template/overview,
gitee代碼地址:https://gitee.com/zunyi-gabe/vue3-pc-template (如果需要簡單版(除了框架啥也沒有)請切到master分支)
演示地址1:https://vue3-pc-template.vercel.app/login演示地址2:https://env-00jxt0stsnl3-static.normal.cloudstatic.cn/index.html
一、準備工作
首先,確保你的 Vue3 項目已經集成了 Vue Router 和合適的 UI 組件庫(如 Element Plus,這里以其為例進行講解,原理相通)。Vue Router 負責管理路由信息,而 UI 組件庫則提供了美觀且功能豐富的菜單組件供我們使用。
二、路由配置基礎
在項目的路由模塊(通常是 router/index.js 之類的文件)中,精心定義好各個路由路徑及其對應的組件。例如:
import { createRouter, createWebHistory } from 'vue-router'; import Home from '@/views/Home.vue'; import About from '@/views/About.vue'; import UserManage from '@/views/UserManage.vue'; const routes = [ { path: '/', name: 'Home', component: Home, meta: { breadcrumbName: '首頁', icon: 'HomeFilled' // 假設 Element Plus 的圖標名稱,實際依庫而定 } }, { path: '/about', name: 'About', component: About, meta: { breadcrumbName: '關于我們', icon: 'InfoFilled' } }, { path: '/user-manage', name: 'UserManage', component: UserManage, meta: { breadcrumbName: '用戶管理', icon: 'UserFilled' }, children: [ { path: 'list', name: 'UserList', component: () => import('@/views/UserList.vue'), meta: { breadcrumbName: '用戶列表' } }, { path: 'add', name: 'UserAdd', component: () => import('@/views/UserAdd.vue'), meta: { breadcrumbName: '添加用戶' } } ] } ]; const router = createRouter({ history: createWebHistory(), routes }); export default router;
這里,每個路由對象都有 meta 字段,用于存儲菜單顯示相關的額外信息,如面包屑名稱和圖標名稱,同時部分路由設置了子路由,構建出層級結構,為側邊菜單的多級展示奠定基礎。
三、組件搭建
創(chuàng)建側邊菜單組件(例如 SidebarMenu.vue):
<template> <div class="logo-container flex-center"> <img class="logo-icon" src="/img/logo.png" /> <text v-if="!isCollapse">后臺管理平臺</text> </div> <el-menu :default-active="$route.path" class="el-menu-vertical-demo" router unique-opened :collapse="isCollapse" @select="changeMenu" > <el-menu-item index="/dashboard"> <el-icon><Menu /></el-icon> <template #title>首頁</template> </el-menu-item> <el-sub-menu v-for="item in routerList" :index="item.path" :key="item.name"> <template #title> <el-icon> <component :is="item.meta.icon" /> </el-icon> <span v-show="!isCollapse"> {{ item.meta.breadcrumbName }} </span> </template> <el-menu-item v-for="ite in item.children" :index="item.path+ '/'+ ite.path" :key="ite.name" >{{ ite.meta.breadcrumbName }}</el-menu-item > </el-sub-menu> </el-menu> </template> <script setup> import router from "@/router"; import { useAuthRouterStore } from "@/stores/authRouter.js"; import { useTagStore } from "@/stores/tagList.js"; const tagStore = useTagStore(); const routerOptions = router.getRoutes() const authRouterStore = useAuthRouterStore(); const props = defineProps(["isCollapse"]); const routerList = authRouterStore.routerList; const changeMenu = (menu) => { let obj = routerOptions.find(val => val.path == menu) tagStore.addTagList({ name:obj.path, breadcrumbName:obj.meta.breadcrumbName }) } </script> <style scoped> .el-menu-vertical-demo:not(.el-menu--collapse) { width: 180; } .logo-container { width: 100%; height: 60px; overflow: hidden; } .logo-icon { height: 60px; scale: 1.4; } </style>
在模板部分:
1、頭部 logo 展示:
通過<div class="logo-container flex-center">包裹,實現了 logo 圖標和平臺名稱的水平居中布局。當側邊欄處于展開狀態(tài)(!isCollapse)時,顯示“后臺管理平臺”文字,logo 圖標通過<img class="logo-icon" src="/img/logo.png" />引入,并且設置了一定的樣式,如高度為 60px,縮放比例為 1.4。
2、 菜單主體構建:
- 使用了 Element UI 的 <el-menu> 組件來構建側邊菜單。
- :default-active="$route.path":將當前激活菜單與當前路由路徑綁定,確保用戶在頁面跳轉時,對應的菜單能正確高亮顯示。
- router 屬性開啟了路由模式,使得點擊菜單能夠自動觸發(fā)路由跳轉。
- unique-opened 保證了同一時間只有一個子菜單處于展開狀態(tài),提升了菜單的交互體驗。
- :collapse="isCollapse":用于控制菜單的折疊狀態(tài),根據傳入的 isCollapse 屬性值來決定菜單是否折疊顯示。
- @select="changeMenu":綁定了菜單選擇事件,當用戶點擊菜單時,會觸發(fā) changeMenu 方法,后續(xù)我們再詳細講解這個方法的作用。
- 菜單分為兩級結構:
一級菜單:<el-menu-item index="/dashboard"> 代表首頁菜單,有對應的圖標<el-icon><Menu /></el-icon>和標題<template #title>首頁</template>。
二級菜單:通過 v-for 循環(huán)遍歷 routerList 數組來生成。每個二級菜單組由 <el-sub-menu> 包裹,標題部分同樣有圖標和名稱顯示,子菜單項通過內層的 v-for 循環(huán) item.children 生成,每個子菜單項的 index 由父級路徑和自身路徑拼接而成,確保路由的準確性,并且展示對應的 breadcrumbName 作為菜單名稱。
在腳本部分:
1、 模塊引入:
- 引入了項目的路由實例 router,這是 Vue Router 在項目中的核心模塊,用于管理路由相關操作。
- 引入了兩個自定義的 Vuex 存儲模塊:useAuthRouterStore 和 useTagStore,分別用于管理認證相關的路由信息和標簽列表信息。實例化 tagStore 和 authRouterStore 以便后續(xù)使用。
2、 組件屬性接收:
- 通過 defineProps(["isCollapse"]) 接收父組件傳入的 isCollapse 屬性,用于控制菜單的折疊狀態(tài),與模板中的 :collapse="isCollapse" 相對應。
3、 數據獲取與方法定義:
- routerList = authRouterStore.routerList:從 authRouterStore 中獲取路由列表數據,這個數據應該是經過權限過濾等處理后的動態(tài)路由列表,用于在模板中生成側邊菜單。
- changeMenu 方法:當菜單被點擊時觸發(fā)。它首先在 routerOptions(通過 router.getRoutes() 獲取的所有路由配置信息)中查找與當前點擊菜單 menu 對應的路由對象 obj。然后,調用 tagStore.addTagList 方法,向標簽列表存儲中添加一個新的標簽對象,包含當前點擊菜單對應的路由路徑 name:obj.path 和面包屑名稱 breadcrumbName:obj.meta.breadcrumbName,這一步可能是用于記錄用戶的操作歷史或者構建面包屑導航等功能。
四、優(yōu)化與拓展
1、 權限控制:結合后端返回的用戶權限信息,在 filteredRoutes 計算屬性中進一步篩選,確保用戶只能看到有權訪問的菜單。例如,可以在路由的 meta 字段添加權限標識,然后根據用戶的權限列表進行比對過濾。
2、 動態(tài)加載菜單:對于大型項目,一次性加載所有菜單可能影響初始加載性能。可以利用 Vue 的異步組件特性,在用戶點擊展開二級菜單或者進入特定模塊時,再動態(tài)加載子菜單對應的組件,優(yōu)化資源利用。
3、 樣式定制:根據項目的設計風格,深入定制側邊菜單的樣式,如顏色、字體、動畫效果等,提升用戶視覺體驗。
通過以上步驟,我們在 Vue3 中成功構建了一個根據路由動態(tài)生成的側邊菜單系統(tǒng),并且為后續(xù)的功能拓展和優(yōu)化鋪就了堅實道路,助力打造高效、易用的 Vue3 應用。
到此這篇關于Vue3 中如何根據路由動態(tài)生成側邊菜單的文章就介紹到這了,更多相關vue3路由動態(tài)生成側邊菜單內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
echarts 使用formatter 修改鼠標懸浮事件信息操作
這篇文章主要介紹了echarts 使用formatter 修改鼠標懸浮事件信息操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-07-07