el-menu動(dòng)態(tài)加載路由的實(shí)現(xiàn)
先看需要實(shí)現(xiàn)的效果

這里有一級(jí)也有二級(jí)菜單,注意二級(jí)菜單的父目錄(”選項(xiàng)設(shè)置“點(diǎn)擊不會(huì)跳轉(zhuǎn),只是展開目錄),然后點(diǎn)擊去詳情頁,需要跳到一個(gè)隱藏的路由,不在菜單展示的路由

還有一點(diǎn)要注意,就是這里有兩個(gè)router-view,整個(gè)頁面是一個(gè)router-view,可以由LoginView和HomeView替換(當(dāng)前看到的頁面),而HomeView下又有一個(gè)router-view,需要用來展示部門,系統(tǒng),超時(shí),員工設(shè)置,不合格品列表和不合格品詳情頁。
以上的信息均需要在數(shù)據(jù)庫的表中體現(xiàn)
先看看直接寫在代碼里需要哪些操作
const routes = [
{
path: '',
name: 'login',
component: LoginView,
}
,
{
component: HomeView,
children: [
{
path: '/home',
name: '不合格品列表',
component: BelowStandard
},
{
path: '/product/:id',
name: '不合格品詳情',
component: BelowStandardDetail
}
]
},
{
component: HomeView,
name: '選項(xiàng)設(shè)置',
children: [
{
path: '/employee',
name: '員工設(shè)置',
component: EmployeeConfig,
},
{
path: '/department',
name: '部門設(shè)置',
component: DepartmentConfig
},
{
path: '/system',
name: '系統(tǒng)設(shè)置',
component: SystemConfig
},
{
path: '/warn',
name: '超時(shí)提醒',
component: WarmConfig
}
]
},
{
component: HomeView,
children: [
{
path: '/statistics',
name: '統(tǒng)計(jì)',
component: DailyStatistics
}
]
},
{
component: HomeView,
children: [
{
path: '/log',
name: '日志管理',
component: LogManager
}
]
},
]
這是路由,當(dāng)要?jiǎng)討B(tài)從數(shù)據(jù)庫加載時(shí),就不能寫在這
<el-menu
router
active-text-color="#ffd04b"
background-color="#000"
class="el-menu-vertical-demo"
:default-active="this.$route.path"
text-color="#fff"
@open=""
@close=""
>
<el-menu-item index="/home">
<template #title>
不合格品列表
</template>
</el-menu-item>
<el-sub-menu index="/subMenuConfig">
<template #title>
選項(xiàng)設(shè)置
</template>
<el-menu-item index="/department">部門設(shè)置</el-menu-item>
<el-menu-item index="/system">系統(tǒng)設(shè)置</el-menu-item>
<el-menu-item index="/warn">超時(shí)設(shè)置</el-menu-item>
<el-menu-item index="/employee">員工設(shè)置</el-menu-item>
</el-sub-menu>
<el-menu-item index="/statistics">
<span>統(tǒng)計(jì)</span>
</el-menu-item>
<el-menu-item index="/log">
<span>日志管理</span>
</el-menu-item>
</el-menu>
這是el-menu開啟了路由功能,所以能跳轉(zhuǎn)路由,當(dāng)動(dòng)態(tài)加載的時(shí)候,這部分需要改造成v-for
數(shù)據(jù)庫

說明:parent_id為0的即是一級(jí)目錄,但是一級(jí)目錄里一部分可以直接展示界面,一部分是展開二級(jí)目錄,我這是以component字段為home/HomeView.vue來區(qū)分是展示二級(jí)目錄。
現(xiàn)在開始寫后端程序,返回菜單的json格式數(shù)據(jù)。

List<Menu> menuList = menuMapper.getMenuByUserId(UserUtils.getLoginUser().getId());
//根據(jù)ParentId分組
Map<Integer, List<Menu>> map = menuList.stream().collect(Collectors.groupingBy(Menu::getParentId, TreeMap::new,Collectors.toList()));
List<Menu> menus = map.get(0);//一級(jí)菜單
menus.forEach(menu->{//給有二級(jí)菜單的目錄設(shè)置children屬性
List<Menu> children = map.get(menu.getId());
menu.setChildren(children);
});
return menus;
從數(shù)據(jù)庫查詢到的數(shù)據(jù)格式如圖,然后分一級(jí)二級(jí)菜單處理后,再返回前端
[
{
"name": "不合格品列表",
"path": "/home",
"component": "product/BelowStandard.vue",
"orderNum": 1,
"parentId": 0,
"isHidden": false,
"children": null
},
{
"name": "選項(xiàng)設(shè)置",
"path": "/subMenuConfig",
"component": "home/HomeView.vue",
"orderNum": 2,
"parentId": 0,
"isHidden": false,
"children": [
{
"name": "員工設(shè)置",
"path": "/employee",
"component": "config/EmployeeConfig.vue",
"orderNum": 1,
"parentId": 2,
"isHidden": false,
"children": null
},
{
"name": "部門設(shè)置",
"path": "/department",
"component": "config/DepartmentConfig.vue",
"orderNum": 2,
"parentId": 2,
"isHidden": false,
"children": null
},
{
"name": "系統(tǒng)設(shè)置",
"path": "/system",
"component": "config/SystemConfig.vue",
"orderNum": 3,
"parentId": 2,
"isHidden": false,
"children": null
},
{
"name": "超時(shí)提醒",
"path": "/warn",
"component": "config/WarmConfig.vue",
"orderNum": 4,
"parentId": 2,
"isHidden": false,
"children": null
}
]
},
{
"name": "統(tǒng)計(jì)",
"path": "/statistics",
"component": "statistics/DailyStatistics.vue",
"orderNum": 3,
"parentId": 0,
"isHidden": false,
"children": null
},
{
"name": "日志管理",
"path": "/log",
"component": "log/LogManager.vue",
"orderNum": 4,
"parentId": 0,
"isHidden": false,
"children": null
},
{
"name": "不合格品詳情",
"path": "/product/:id",
"component": "product/BelowStandardDetail.vue",
"orderNum": 5,
"parentId": 0,
"isHidden": true,
"children": null
}
]
前端得到數(shù)據(jù)之后進(jìn)行處理,再添加到路由,過程中遇到一個(gè)問題,vue-router4版本去掉addRoutes換成addRoute帶來的問題困擾我很久
用Vue3就必須用Router4.x版本,由于4.0去掉了addRoutes 所以只能用addRoute
現(xiàn)在是只能添加一個(gè)
function routerPackag(routers:any) {
if (routers) {
routers.filter((itemRouter:any) => {
if (itemRouter.component != "Layout") {
router.addRoute('home',{ //home是父組件 add-route添加進(jìn)父組件chilren里
path: `${itemRouter.path}`,
name: itemRouter.name,
meta: {
title: itemRouter.name,
},
component: () => import(`../views/${itemRouter.component}`),
})
}
if (itemRouter.children && itemRouter.children.length) {
routerPackag(itemRouter.children)
}
return true
})
}
}
初始化路由:
router.beforeEach((to, from, next) => {//配置路由守衛(wèi)
if(to.path==='/'){
next()
}else if(store.state.user.id){
initMenus(router,store,next,to)
}else{
next({ path: '/',query: {redirect: to.path}});
}
});
export const initMenus = (router, store,next,to) => {//按F5刷新的話vuex里的會(huì)被清空,長度變?yōu)?
if (store.state.menu !== null) {
next()
}else {
axios.get("/menu").then(response => {
if (response) {
let responseData = response.data
if (responseData.flag) {
store.state.menu = responseData.data
initRoute(router,store.state)
next({...to,replace:true})//解決router4版本的第一次路由不匹配問題
} else {
this.$ElMessage.error('請(qǐng)求菜單失敗')
}
}
})
}
}
const initRoute = (router,state)=> {
const loadView = view => {//這種引入方式控制臺(tái)不會(huì)報(bào)警告
// 路由懶加載
return () => import(`@/views/${view}`)
};
const menus = state.menu
const firstLevelMenu = {
children: [],
component: loadView('home/HomeView.vue')
}
menus.forEach(menu=>{
menu.component = loadView(menu.component)
if(menu.children === null || menu.children.length === 0){
firstLevelMenu.children.push(menu)
}else{
menu.children.forEach(children=>{
children.component = loadView(children.component)
})
router.addRoute(menu)
}
})
router.addRoute(firstLevelMenu)
}
完成這些配置之后,路由就能動(dòng)態(tài)加載了,然后取出vuex中存儲(chǔ)的menu生成el-menu

vuex中菜單大致如圖
<el-menu
router
active-text-color="#ffd04b"
background-color="#000"
class="el-menu-vertical-demo"
:default-active="this.$route.path"
text-color="#fff"
@open=""
@close=""
>
<template v-for="route of this.$store.state.menu">
<template v-if="route.children === null || route.children.length === 0"><!--一級(jí)菜單-->
<template v-if="!route.isHidden">
<el-menu-item :index = "route.path">
<span>{{route.name}}</span>
</el-menu-item>
</template>
</template>
<template v-else><!--二級(jí)菜單-->
<template v-if="!route.isHidden">
<el-sub-menu :index = "route.path">
<template #title>
<span>{{route.name}}</span>
</template>
<template v-for="children of route.children">
<template v-if="!children.isHidden">
<el-menu-item :index = "children.path">
<span>{{children.name}}</span>
</el-menu-item>
</template>
</template>
</el-sub-menu>
</template>
</template>
</template>
</el-menu>
實(shí)現(xiàn)效果展示


到此這篇關(guān)于el-menu動(dòng)態(tài)加載路由的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)el-menu動(dòng)態(tài)加載路由內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue通過字符串關(guān)鍵字符實(shí)現(xiàn)動(dòng)態(tài)渲染input輸入框
這篇文章主要為大家詳細(xì)介紹了Vue如何通過字符串關(guān)鍵字符實(shí)現(xiàn)動(dòng)態(tài)渲染input輸入框。文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2022-12-12
Vue實(shí)現(xiàn)Excel本地下載及上傳的方法詳解
相信大家在項(xiàng)目中經(jīng)常會(huì)遇到一些上傳下載文件的相關(guān)功能。這篇文章將為大家介紹一下Vue實(shí)現(xiàn)Excel本地下載及上傳的示例代碼,需要的可以參考一下2022-07-07
vue-element如何實(shí)現(xiàn)動(dòng)態(tài)換膚存儲(chǔ)
這篇文章主要介紹了vue-element如何實(shí)現(xiàn)動(dòng)態(tài)換膚存儲(chǔ)問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-04-04
vue+webpack實(shí)現(xiàn)異步組件加載的方法
下面小編就為大家分享一篇vue+webpack實(shí)現(xiàn)異步組件加載的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-02-02
Vue項(xiàng)目中禁用ESLint的幾種常見方法小結(jié)
Vue ESLint是一個(gè)基于ESLint的插件,它專門為Vue.js應(yīng)用設(shè)計(jì),用于提供JavaScript代碼風(fēng)格檢查和最佳實(shí)踐規(guī)則,Vue項(xiàng)目通常會(huì)集成ESLint,目的是為了提升代碼質(zhì)量、保持一致性和可維護(hù)性,本文介紹了Vue項(xiàng)目中禁用ESLint的幾種常見方法,需要的朋友可以參考下2024-07-07
解決vue?app.js/vender.js過大優(yōu)化啟動(dòng)頁
這篇文章主要為大家介紹了解決vue?app.js/vender.js過大優(yōu)化啟動(dòng)頁過程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-10-10
vue點(diǎn)擊單張圖片放大實(shí)現(xiàn)步驟(純js)
這篇文章主要給大家介紹了關(guān)于vue點(diǎn)擊單張圖片放大實(shí)現(xiàn)的相關(guān)資料,在vue項(xiàng)目中實(shí)現(xiàn)點(diǎn)擊圖片放大功能相信對(duì)大家來說都不陌生,文中給出了詳細(xì)的js示例代碼,需要的朋友可以參考下2023-07-07
解決vue動(dòng)態(tài)為數(shù)據(jù)添加新屬性遇到的問題
今天小編就為大家分享一篇解決vue動(dòng)態(tài)為數(shù)據(jù)添加新屬性遇到的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-09-09

