Commit 44cb8fff authored by tyyin lan's avatar tyyin lan

refactor: 路由配置

parent 09e28fcd
...@@ -2,21 +2,22 @@ ...@@ -2,21 +2,22 @@
import { sidebarMenus } from '@/router/index' import { sidebarMenus } from '@/router/index'
import { useDesignSettingStore } from '@/store/modules/design-setting' import { useDesignSettingStore } from '@/store/modules/design-setting'
import SidebarLogo from '../sidebar-logo/sidebar-logo.vue' import SidebarLogo from '../sidebar-logo/sidebar-logo.vue'
// import { type MenuOption } from '@/router/utils' import { type MenuOption } from '@/router/utils'
import { useRoute, useRouter } from 'vue-router' import { useRoute, useRouter } from 'vue-router'
import { ref, watch } from 'vue' import { ref, watch } from 'vue'
import EllipsisTooltipText from '@/components/ellipsis-tooltip-text.vue' import EllipsisTooltipText from '@/components/ellipsis-tooltip-text.vue'
import { ElMessage } from 'element-plus'
const designSettingStore = useDesignSettingStore() const designSettingStore = useDesignSettingStore()
const currentRoute = useRoute() const currentRoute = useRoute()
const router = useRouter() const router = useRouter()
const menuValue = ref(currentRoute.meta.key) const menuValue = ref<string>((currentRoute.meta.key as string) || '')
watch( watch(
() => currentRoute.fullPath, () => currentRoute.fullPath,
() => { () => {
menuValue.value = currentRoute.meta.key menuValue.value = currentRoute.meta.key as string
}, },
) )
...@@ -24,8 +25,14 @@ function handleLogoClick() { ...@@ -24,8 +25,14 @@ function handleLogoClick() {
router.push({ name: 'Root' }) router.push({ name: 'Root' })
} }
function handleMenuItemClick(routeName: any) { function handleMenuItemClick(menuInfo: MenuOption) {
router.push({ name: routeName }) if (menuInfo.routeName) {
router.push({ name: menuInfo.routeName })
} else if (menuInfo.path) {
router.push(menuInfo.path)
} else {
ElMessage.error('菜单项配置错误')
}
} }
</script> </script>
...@@ -42,12 +49,12 @@ function handleMenuItemClick(routeName: any) { ...@@ -42,12 +49,12 @@ function handleMenuItemClick(routeName: any) {
class="!border-none" class="!border-none"
> >
<template v-for="menuInfo in sidebarMenus"> <template v-for="menuInfo in sidebarMenus">
<template v-if="!menuInfo.children"> <template v-if="!menuInfo.children || menuInfo.children.length === 0">
<el-menu-item <el-menu-item
:key="menuInfo.key" :key="menuInfo.key"
:index="menuInfo.key" :index="menuInfo.key"
class="relative hover:!bg-[unset]" class="relative hover:!bg-[unset]"
@click="handleMenuItemClick(menuInfo.routeName)" @click="handleMenuItemClick(menuInfo)"
> >
<span <span
class="absolute inset-x-[10px] inset-y-[4px] rounded-[6px] bg-[#e5f6fd] opacity-0 transition" class="absolute inset-x-[10px] inset-y-[4px] rounded-[6px] bg-[#e5f6fd] opacity-0 transition"
...@@ -102,7 +109,7 @@ function handleMenuItemClick(routeName: any) { ...@@ -102,7 +109,7 @@ function handleMenuItemClick(routeName: any) {
<el-menu-item <el-menu-item
:index="menuChildInfo.key" :index="menuChildInfo.key"
class="group relative hover:!bg-[unset]" class="group relative hover:!bg-[unset]"
@click="handleMenuItemClick(menuChildInfo.routeName)" @click="handleMenuItemClick(menuChildInfo)"
> >
<span <span
class="absolute inset-x-[10px] inset-y-[4px] rounded-[6px] bg-[#e5f6fd] opacity-0 transition" class="absolute inset-x-[10px] inset-y-[4px] rounded-[6px] bg-[#e5f6fd] opacity-0 transition"
......
...@@ -42,10 +42,12 @@ watchEffect(() => { ...@@ -42,10 +42,12 @@ watchEffect(() => {
<NavBar class="h-full" /> <NavBar class="h-full" />
</el-header> </el-header>
<el-main class="main-content-wrapper"> <el-main class="main-content-wrapper !overflow-hidden">
<RouterView v-slot="{ Component }"> <RouterView v-slot="{ Component, route }">
<Transition appear name="fade-slide" mode="out-in"> <Transition appear name="fade-slide" mode="out-in">
<Component :is="Component" /> <div :key="route.fullPath" class="h-full overflow-hidden">
<Component :is="Component" />
</div>
</Transition> </Transition>
</RouterView> </RouterView>
</el-main> </el-main>
......
...@@ -3,12 +3,11 @@ import { nanoid } from 'nanoid/non-secure' ...@@ -3,12 +3,11 @@ import { nanoid } from 'nanoid/non-secure'
import { createWebHashHistory, createWebHistory, type RouteRecordRaw, type RouterHistory } from 'vue-router' import { createWebHashHistory, createWebHistory, type RouteRecordRaw, type RouterHistory } from 'vue-router'
export interface MenuOption { export interface MenuOption {
// label: (() => VNode) | string
label: string label: string
key: string key: string
routeName: string routeName: string
// icon: (() => VNode) | null
icon: string icon: string
path: string
children?: MenuOption[] children?: MenuOption[]
} }
...@@ -37,16 +36,20 @@ function menuSort(routes: RouteRecordRaw[]) { ...@@ -37,16 +36,20 @@ function menuSort(routes: RouteRecordRaw[]) {
} }
export function menuFilterSort(routes: RouteRecordRaw[]) { export function menuFilterSort(routes: RouteRecordRaw[]) {
function createRouteKey(routes: RouteRecordRaw[]) { function createRouteKey(
routes: RouteRecordRaw[],
parentRoute: (RouteRecordRaw & { meta: { key: string } }) | null = null,
) {
routes.forEach((route, index) => { routes.forEach((route, index) => {
if (route.meta) { if (route.meta) {
route.meta.key = nanoid() route.meta.key =
route.meta.hideSideMenItem && parentRoute && parentRoute.meta.key ? parentRoute.meta.key : nanoid()
} else { } else {
route.meta = { key: nanoid(), title: '', rank: 1001 + index } route.meta = { key: parentRoute?.meta?.key || nanoid(), title: '', rank: 1001 + index }
} }
if (route.children) { if (route.children) {
createRouteKey(route.children) createRouteKey(route.children, route as RouteRecordRaw & { meta: { key: string } })
} }
}) })
} }
...@@ -73,7 +76,7 @@ export function menuFilterSort(routes: RouteRecordRaw[]) { ...@@ -73,7 +76,7 @@ export function menuFilterSort(routes: RouteRecordRaw[]) {
routes.unshift(...rootRouteChildren) routes.unshift(...rootRouteChildren)
} }
function createMenuOptions(routes: RouteRecordRaw[]) { function createMenuOptions(routes: RouteRecordRaw[], parentRoute: RouteRecordRaw | null = null) {
const menuOptions: MenuOption[] = [] const menuOptions: MenuOption[] = []
routes.forEach((route) => { routes.forEach((route) => {
...@@ -83,18 +86,15 @@ export function menuFilterSort(routes: RouteRecordRaw[]) { ...@@ -83,18 +86,15 @@ export function menuFilterSort(routes: RouteRecordRaw[]) {
} }
const menuOption: MenuOption = { const menuOption: MenuOption = {
// label: route.meta?.title
// ? () => h(RouterLink, { to: { name: route.name || 'Root' } }, { default: () => route.meta?.title })
// : '-',
label: (route.meta?.title as string) || '-', label: (route.meta?.title as string) || '-',
key: route.meta?.key as string, key: route.meta?.key as string,
routeName: route.name as string, routeName: route.name as string,
// icon: route.meta?.icon ? () => h('i', { class: `iconfont ${route?.meta?.icon as string}` }) : null,
icon: route.meta?.icon || '', icon: route.meta?.icon || '',
path: (route.path = route.path.startsWith('/') ? route.path : `${parentRoute?.path}/${route.path}`),
} }
if (route.children) { if (route.children) {
menuOption.children = createMenuOptions(route.children) menuOption.children = createMenuOptions(route.children, route)
} }
menuOptions.push(menuOption) menuOptions.push(menuOption)
......
...@@ -5,7 +5,8 @@ export {} ...@@ -5,7 +5,8 @@ export {}
declare module 'vue-router' { declare module 'vue-router' {
interface RouteMeta { interface RouteMeta {
rank: number rank: number
title: string title?: string
icon?: string icon?: string
hideSideMenItem?: boolean
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment