菜单路由动态加载

This commit is contained in:
骑着蜗牛追导弹 2024-02-05 13:07:15 +08:00
parent 30edf5ba75
commit c544a40cf1
5 changed files with 41 additions and 41 deletions

View File

@ -3,6 +3,9 @@ import menuMixin from '../mixin/menu'
import { createMenu } from '../libs/util.menu' import { createMenu } from '../libs/util.menu'
import BScroll from 'better-scroll' import BScroll from 'better-scroll'
/**
* 渲染侧栏菜单
*/
export default { export default {
name: 'd2-layout-header-aside-menu-side', name: 'd2-layout-header-aside-menu-side',
mixins: [ mixins: [

View File

@ -9,12 +9,27 @@ import store from '@/store/index'
// 菜单和路由设置 // 菜单和路由设置
import router from './router' import router from './router'
import { menuHeader, menuAside } from '@/menu'
import { frameInRoutes } from '@/router/routes' import { frameInRoutes } from '@/router/routes'
import { uniqueId } from 'lodash'
// 核心插件 // 核心插件
Vue.use(d2Admin) Vue.use(d2Admin)
/**
* @description 给菜单数据补充上 path 字段
* @description https://github.com/d2-projects/d2-admin/issues/209
* @param {Array} menu 原始的菜单数据
*/
function supplementPath (menu) {
return menu.map(e => ({
...e,
path: e.path || uniqueId('d2-menu-empty-'),
...e.children ? {
children: supplementPath(e.children)
} : {}
}))
}
new Vue({ new Vue({
router, router,
store, store,
@ -24,9 +39,9 @@ new Vue({
// 处理路由 得到每一级的路由设置 // 处理路由 得到每一级的路由设置
this.$store.commit('d2admin/page/init', frameInRoutes) this.$store.commit('d2admin/page/init', frameInRoutes)
// 设置顶栏菜单 // 设置顶栏菜单
this.$store.commit('d2admin/menu/headerSet', menuHeader) this.$store.commit('d2admin/menu/headerSet', supplementPath([]))
// 设置侧边栏菜单 // 设置侧边栏菜单
this.$store.commit('d2admin/menu/asideSet', menuAside) this.$store.commit('d2admin/menu/asideSet', supplementPath([]))
// 初始化菜单搜索功能 // 初始化菜单搜索功能
this.$store.commit('d2admin/search/init', menuHeader) this.$store.commit('d2admin/search/init', menuHeader)
}, },

View File

@ -1,22 +0,0 @@
import { uniqueId } from 'lodash'
/**
* @description 给菜单数据补充上 path 字段
* @description https://github.com/d2-projects/d2-admin/issues/209
* @param {Array} menu 原始的菜单数据
*/
function supplementPath (menu) {
return menu.map(e => ({
...e,
path: e.path || uniqueId('d2-menu-empty-'),
...e.children ? {
children: supplementPath(e.children)
} : {}
}))
}
const menuData = []
// export const menuHeader = supplementPath(menuData)
export const menuHeader = []
export const menuAside = supplementPath(menuData)

View File

@ -16,7 +16,7 @@ export default {
async login ({ dispatch }, { async login ({ dispatch }, {
username = '', username = '',
password = '', password = '',
callback = (defaultMenu, menuTree) => {} callback = (menuTree) => {}
} = {}) { } = {}) {
const res = await api.login({ username, password }) const res = await api.login({ username, password })
// 设置 cookie 一定要存 uuid 和 token 两个 cookie // 设置 cookie 一定要存 uuid 和 token 两个 cookie
@ -30,9 +30,8 @@ export default {
util.cookies.set('token', res.token) util.cookies.set('token', res.token)
// 设置路由菜单 // 设置路由菜单
console.log('========= 设置菜单 ======== ') console.log('========= 设置菜单 ======== ')
const defaultMenu = [{ path: '/index', title: '首页', icon: 'home' }]
const menuTree = await api.queryAllMenus({}) const menuTree = await api.queryAllMenus({})
callback(defaultMenu, menuTree) callback(menuTree)
// 设置 vuex 用户信息 // 设置 vuex 用户信息
await dispatch('d2admin/user/set', { name: res.name }, { root: true }) await dispatch('d2admin/user/set', { name: res.name }, { root: true })
// 用户登录后从持久化数据加载一系列的设置 // 用户登录后从持久化数据加载一系列的设置

View File

@ -188,21 +188,26 @@ export default {
} }
}) })
}, },
loginSuccessCallback (defaultMenu, menuTree) { loginSuccessCallback (menuTree) {
let menuData = [] let menuData = [{ path: '/index', title: '首页', icon: 'home' }]
const rootMenu = [] //
const d2adminMenus = []
if (menuTree && menuTree.length > 0) { if (menuTree && menuTree.length > 0) {
for (const menuItem in menuTree) { for (const menuItem of menuTree) {
console.log('=============== menuItem ', menuItem)
let menuTemp let menuTemp
// //
if (menuItem.menuPath) { if (menuItem.menuPath) {
menuTemp = { title: menuItem.menuTitle, ico: menuItem.menuIcon, path: menuItem.menuPath } menuTemp = { id: menuItem.id, title: menuItem.menuTitle, ico: menuItem.menuIcon, path: menuItem.menuPath }
} else { } else {
menuTemp = { title: menuItem.menuTitle, ico: menuItem.menuIcon } // pathundefined, 'src/layout/header-aside/components/libs/util.menu.js'
menuTemp = { id: menuItem.id, title: menuItem.menuTitle, ico: menuItem.menuIcon, path: '' }
} }
if (menuItem.children && menuItem.children.length > 0) { //
for (const childrenMenuItem in menuItem.children) { const children = menuItem.children;
const childrenMenuTemp = { title: childrenMenuItem.menuTitle, ico: childrenMenuItem.menuIcon, path: childrenMenuItem.menuPath } if (children && children.length > 0) {
for (const childrenMenuItem of children) {
const childrenMenuTemp = { id: menuItem.id, title: childrenMenuItem.menuTitle, ico: childrenMenuItem.menuIcon, path: childrenMenuItem.menuPath }
if (menuTemp.children && menuTemp.children.length > 0) { if (menuTemp.children && menuTemp.children.length > 0) {
menuTemp.children.push(childrenMenuTemp) menuTemp.children.push(childrenMenuTemp)
} else { } else {
@ -211,11 +216,11 @@ export default {
} }
} }
} }
rootMenu.push(menuTemp) d2adminMenus.push(menuTemp)
} }
} }
menuData = menuData.concat(defaultMenu) //
menuData = menuData.concat(rootMenu) menuData = menuData.concat(d2adminMenus)
this.$store.commit('d2admin/menu/asideSet', menuData) this.$store.commit('d2admin/menu/asideSet', menuData)
} }
} }