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

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

還有一點要注意,就是這里有兩個router-view,整個頁面是一個router-view,可以由LoginView和HomeView替換(當(dāng)前看到的頁面),而HomeView下又有一個router-view,需要用來展示部門,系統(tǒng),超時,員工設(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: '選項設(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: '超時提醒',
component: WarmConfig
}
]
},
{
component: HomeView,
children: [
{
path: '/statistics',
name: '統(tǒng)計',
component: DailyStatistics
}
]
},
{
component: HomeView,
children: [
{
path: '/log',
name: '日志管理',
component: LogManager
}
]
},
]
這是路由,當(dāng)要動態(tài)從數(shù)據(jù)庫加載時,就不能寫在這
<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>
選項設(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è)置</el-menu-item>
<el-menu-item index="/employee">員工設(shè)置</el-menu-item>
</el-sub-menu>
<el-menu-item index="/statistics">
<span>統(tǒng)計</span>
</el-menu-item>
<el-menu-item index="/log">
<span>日志管理</span>
</el-menu-item>
</el-menu>
這是el-menu開啟了路由功能,所以能跳轉(zhuǎn)路由,當(dāng)動態(tài)加載的時候,這部分需要改造成v-for
數(shù)據(jù)庫

說明:parent_id為0的即是一級目錄,但是一級目錄里一部分可以直接展示界面,一部分是展開二級目錄,我這是以component字段為home/HomeView.vue來區(qū)分是展示二級目錄。
現(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);//一級菜單
menus.forEach(menu->{//給有二級菜單的目錄設(shè)置children屬性
List<Menu> children = map.get(menu.getId());
menu.setChildren(children);
});
return menus;
從數(shù)據(jù)庫查詢到的數(shù)據(jù)格式如圖,然后分一級二級菜單處理后,再返回前端
[
{
"name": "不合格品列表",
"path": "/home",
"component": "product/BelowStandard.vue",
"orderNum": 1,
"parentId": 0,
"isHidden": false,
"children": null
},
{
"name": "選項設(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": "超時提醒",
"path": "/warn",
"component": "config/WarmConfig.vue",
"orderNum": 4,
"parentId": 2,
"isHidden": false,
"children": null
}
]
},
{
"name": "統(tǒng)計",
"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)行處理,再添加到路由,過程中遇到一個問題,vue-router4版本去掉addRoutes換成addRoute帶來的問題困擾我很久
用Vue3就必須用Router4.x版本,由于4.0去掉了addRoutes 所以只能用addRoute
現(xiàn)在是只能添加一個
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里的會被清空,長度變?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('請求菜單失敗')
}
}
})
}
}
const initRoute = (router,state)=> {
const loadView = view => {//這種引入方式控制臺不會報警告
// 路由懶加載
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)
}
完成這些配置之后,路由就能動態(tài)加載了,然后取出vuex中存儲的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"><!--一級菜單-->
<template v-if="!route.isHidden">
<el-menu-item :index = "route.path">
<span>{{route.name}}</span>
</el-menu-item>
</template>
</template>
<template v-else><!--二級菜單-->
<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>
實現(xiàn)效果展示


到此這篇關(guān)于el-menu動態(tài)加載路由的實現(xiàn)的文章就介紹到這了,更多相關(guān)el-menu動態(tài)加載路由內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue通過字符串關(guān)鍵字符實現(xiàn)動態(tài)渲染input輸入框
這篇文章主要為大家詳細(xì)介紹了Vue如何通過字符串關(guān)鍵字符實現(xiàn)動態(tài)渲染input輸入框。文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2022-12-12
vue-element如何實現(xiàn)動態(tài)換膚存儲
這篇文章主要介紹了vue-element如何實現(xiàn)動態(tài)換膚存儲問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-04-04
解決vue?app.js/vender.js過大優(yōu)化啟動頁
這篇文章主要為大家介紹了解決vue?app.js/vender.js過大優(yōu)化啟動頁過程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-10-10
解決vue動態(tài)為數(shù)據(jù)添加新屬性遇到的問題
今天小編就為大家分享一篇解決vue動態(tài)為數(shù)據(jù)添加新屬性遇到的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-09-09

