Skip to content

Commit 4d63670

Browse files
author
sunshine824
committed
代码优化
1 parent 8722e47 commit 4d63670

File tree

5 files changed

+71
-25
lines changed

5 files changed

+71
-25
lines changed

src/components/ABreadCrumb/index.tsx

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,23 @@
1-
import { defineComponent } from 'vue'
1+
import { defineComponent, watch, computed, onMounted } from 'vue'
2+
import { useRoute } from 'vue-router'
23
import { Breadcrumb } from 'ant-design-vue'
34

45
import styles from './index.module.less'
56

67
const ABreadCrumb = defineComponent({
78
name: 'ABreadCrumb',
89
setup({ props }) {
10+
const route = useRoute()
11+
12+
onMounted(() => {
13+
console.log(breadList)
14+
})
15+
16+
// 监听路由变化
17+
const breadList = computed(() => {
18+
const paths = route.path.split('/')
19+
})
20+
921
return () => (
1022
<div class={styles['bread-crumb']}>
1123
<span class={styles['location']}>当前位置:</span>

src/components/GlobalHeader/LevelMenus.tsx

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { defineComponent, ref, watch } from 'vue'
1+
import { defineComponent, onMounted, ref, watch } from 'vue'
22
import { RouteRecordRaw, useRoute, useRouter } from 'vue-router'
33
import router from '../../router'
44

@@ -17,7 +17,14 @@ const LevelMenus = defineComponent({
1717

1818
const router = useRouter()
1919

20-
const baseUrl = ref(route.path.split('/')) // 当前跳转基础路由
20+
const baseUrl = ref() // 当前跳转基础路由
21+
22+
onMounted(() => {
23+
const paths = route.path.split('/')
24+
if (paths.length > 1) {
25+
baseUrl.value = '/' + paths[1]
26+
}
27+
})
2128

2229
// 路由跳转
2330
const skipTo = (item: RouteRecordRaw) => {
@@ -26,7 +33,10 @@ const LevelMenus = defineComponent({
2633

2734
// 监听路由变化
2835
watch(route, (val) => {
29-
baseUrl.value = val.path.split('/')
36+
const paths = val.path.split('/')
37+
if (paths.length > 1) {
38+
baseUrl.value = '/' + paths[1]
39+
}
3040
})
3141

3242
return () => (
@@ -36,7 +46,7 @@ const LevelMenus = defineComponent({
3646
return (
3747
<li
3848
onClick={skipTo.bind(this, menu)}
39-
class={menu.name == baseUrl.value[1] && styles['active']}
49+
class={menu.path == baseUrl.value && styles['active']}
4050
>
4151
<span class={`iconfont ${menu?.meta?.icon} menu-icon`}></span>
4252
<span class={styles['title']}>{menu?.meta?.title}</span>

src/components/GlobalHeader/Menus.tsx

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { defineComponent, ref, watch, computed } from 'vue'
1+
import { defineComponent, ref, watch, computed, onMounted } from 'vue'
22
import { useRoute, useRouter, RouteRecordRaw } from 'vue-router'
33
import { Menu } from 'ant-design-vue'
44

@@ -19,7 +19,12 @@ const Menus = defineComponent({
1919
setup(props, { slots }) {
2020
const route = useRoute()
2121
const router = useRouter()
22-
const activeRoute = ref(route.path) // 当前路由
22+
const activeRoute = ref() // 当前路由
23+
24+
onMounted(() => {
25+
const paths = route.path.split('/')
26+
activeRoute.value = paths[paths.length - 1]
27+
})
2328

2429
// 默认展开项
2530
const openKeys = computed(() => {
@@ -36,13 +41,14 @@ const Menus = defineComponent({
3641

3742
// 监听路由变化
3843
watch(route, (val) => {
39-
activeRoute.value = val.path
44+
const paths = val.path.split('/')
45+
activeRoute.value = paths[paths.length - 1]
4046
})
4147

4248
// 子级导航渲染
4349
const SubItem = (menus: any) => {
4450
return (
45-
<Menu.SubMenu key={menus.path}>
51+
<Menu.SubMenu key={menus.name}>
4652
{{
4753
title: () => (
4854
<>
@@ -57,7 +63,7 @@ const Menus = defineComponent({
5763
{menus.children.map((menu: any) => {
5864
if (!menu.children || !menu.children.length) {
5965
return (
60-
<Menu.Item key={menu.path}>
66+
<Menu.Item key={menu.name}>
6167
{menus.meta?.icon && (
6268
<span
6369
class={`iconfont ${menu.meta.icon} menu-icon`}
@@ -92,7 +98,7 @@ const Menus = defineComponent({
9298
{(props.menuLists as RouteRecordRaw[]).map((menu) => {
9399
if (!menu.children || !menu.children.length) {
94100
return (
95-
<Menu.Item key={menu.path}>
101+
<Menu.Item key={menu.name}>
96102
{menu.meta?.icon && (
97103
<span class={`iconfont ${menu.meta.icon} menu-icon`}></span>
98104
)}

src/mock/user/permission.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,37 @@ const BaseUrl = import.meta.env.VITE_API_BASE_URL as string
55

66
const Roles = [
77
{
8-
url: 'database',
8+
moduleUrl: '/database', // 模块地址
9+
url: '/database', // 路由地址
910
name: '数据库配置',
1011
icon: 'icon-zichan',
1112
},
1213
{
13-
url: 'eoms',
14+
moduleUrl: '/eoms',
15+
url: '/eoms',
1416
name: '运维配置',
1517
icon: 'icon-yunwei1',
1618
list: [
1719
{
1820
name: '故障处理方案',
19-
url: 'eoms/fault',
21+
moduleUrl: '/eoms/fault',
22+
url: 'fault',
2023
icon: '',
2124
},
2225
{
26+
moduleUrl: '/eoms/alarm',
27+
url: 'alarm',
2328
name: '告警模板管理',
24-
url: 'eoms/alarm',
2529
icon: '',
2630
},
2731
],
2832
},
29-
{ name: '物联管理', url: 'smart', icon: 'icon-wulianwang' },
33+
{
34+
moduleUrl: '/smart',
35+
url: '/smart',
36+
name: '物联管理',
37+
icon: 'icon-wulianwang',
38+
},
3039
]
3140

3241
Mock.mock(`${BaseUrl}/navPerson`, 'get', () => {

src/router/index.ts

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {
22
createRouter,
33
createWebHashHistory,
4+
createWebHistory,
45
RouteRecordRaw,
56
RouteLocationNormalized,
67
NavigationGuardNext,
@@ -15,7 +16,7 @@ let isAddDynamicMenuRoutes = false // 是否请求路由表
1516

1617
// 路由实例
1718
const router: Router = createRouter({
18-
history: createWebHashHistory(),
19+
history: createWebHistory(),
1920
routes: baseRoutes,
2021
})
2122

@@ -27,11 +28,9 @@ function fnAddDynamicMenuRoutes(
2728
menuList.forEach((item) => {
2829
if (!item.list) {
2930
routes.push({
30-
path: `/${item.url}`,
31-
name: item.url,
32-
//component: (resolve: any) => require([`@/views/${item.url}/index`], resolve),
33-
component: () =>
34-
import(/* webpackChunkName: "login" */ `../views/${item.url}/index`),
31+
path: `${item.url}`,
32+
name: item.url.slice(0, 1) == '/' ? item.url.slice(1) : item.url, // 截取开头"/"
33+
component: () => import('../views' + item.moduleUrl + '/index'),
3534
meta: {
3635
title: item.name,
3736
hidden: false,
@@ -41,10 +40,10 @@ function fnAddDynamicMenuRoutes(
4140
} else if (item.list && item.list.length) {
4241
const menus = fnAddDynamicMenuRoutes(item.list, [])
4342
routes.push({
44-
path: `/${item.url}`,
45-
name: item.url,
43+
path: `${item.url}`,
44+
name: item.url.slice(0, 1) == '/' ? item.url.slice(1) : item.url, // 截取开头"/"
4645
component: RouteLayout,
47-
redirect: menus[0].path,
46+
redirect: item.url + '/' + menus[0].path,
4847
children: menus,
4948
meta: {
5049
title: item.name,
@@ -78,11 +77,17 @@ router.beforeEach(
7877
menu.menuList || [],
7978
[],
8079
)
80+
// 添加兜底路由
81+
menuRoutes.push({
82+
path: '/:pathMatch(.*)*',
83+
redirect: { name: '404' },
84+
})
8185
mainRoutes.children?.unshift(...menuRoutes)
8286
// 动态添加路由
8387
router.addRoute(mainRoutes)
8488
// 注:这步很关键,不然导航获取不到路由
8589
router.options.routes.unshift(mainRoutes)
90+
console.log(router.options)
8691
// 本地存储按钮权限集合
8792
sessionStorage.setItem(
8893
'permissions',
@@ -122,4 +127,8 @@ router.beforeEach(
122127
},
123128
)
124129

130+
router.onError((handler) => {
131+
console.log(handler)
132+
})
133+
125134
export default router

0 commit comments

Comments
 (0)