Skip to content

Commit 6309089

Browse files
committed
fix: add menu sort
1 parent 9d21419 commit 6309089

File tree

8 files changed

+124
-106
lines changed

8 files changed

+124
-106
lines changed

package.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,22 +33,22 @@
3333
"mockjs": "^1.1.0",
3434
"nprogress": "^1.0.0-1",
3535
"pinia": "2.0.9",
36-
"qs": "^6.10.2",
36+
"qs": "^6.10.3",
3737
"socket.io-client": "4.4.1",
3838
"sortablejs": "^1.14.0",
3939
"vue": "^3.2.26",
40-
"vue-i18n": "^9.2.0-beta.26",
40+
"vue-i18n": "^9.2.0-beta.28",
4141
"vue-router": "^4.0.12",
42-
"xlsx": "^0.17.4"
42+
"xlsx": "^0.17.5"
4343
},
4444
"devDependencies": {
4545
"@commitlint/cli": "^16.0.2",
4646
"@commitlint/config-conventional": "^16.0.0",
4747
"@types/lodash-es": "^4.17.5",
4848
"@types/node": "^17.0.8",
4949
"@types/webpack-env": "^1.16.3",
50-
"@typescript-eslint/eslint-plugin": "^5.9.0",
51-
"@typescript-eslint/parser": "^5.9.0",
50+
"@typescript-eslint/eslint-plugin": "^5.9.1",
51+
"@typescript-eslint/parser": "^5.9.1",
5252
"@vue/cli-plugin-babel": "^5.0.0-rc.1",
5353
"@vue/cli-plugin-eslint": "^5.0.0-rc.1",
5454
"@vue/cli-plugin-router": "^5.0.0-rc.1",
@@ -64,7 +64,7 @@
6464
"eslint": "^8.6.0",
6565
"eslint-config-prettier": "^8.3.0",
6666
"eslint-plugin-prettier": "^4.0.0",
67-
"eslint-plugin-vue": "^8.2.0",
67+
"eslint-plugin-vue": "^8.3.0",
6868
"husky": "^7.0.4",
6969
"less": "^4.1.2",
7070
"less-loader": "10.2.0",

src/layout/menu/menu-item.vue

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
<TitleI18n :title="props.menuInfo?.meta?.title" />
1313
</span>
1414
</template>
15-
<template v-for="item in props.menuInfo?.children" :key="item.name">
15+
<template v-for="item in menuChildren" :key="item.name">
1616
<!-- 递归生成菜单 -->
1717
<MyMenuItem :menuInfo="item" />
1818
</template>
@@ -34,7 +34,7 @@
3434
</script>
3535

3636
<script setup lang="ts">
37-
import { PropType } from 'vue';
37+
import { type PropType, computed } from 'vue';
3838
import { Menu } from 'ant-design-vue';
3939
import type { RouteRecordRaw } from 'vue-router';
4040
import { IconFont } from '@/components/basic/iconfont';
@@ -45,6 +45,12 @@
4545
type: Object as PropType<RouteRecordRaw>,
4646
},
4747
});
48+
49+
const menuChildren = computed(() =>
50+
[...(props.menuInfo?.children || [])].sort(
51+
(a, b) => (a?.meta?.orderNum || 0) - (b?.meta?.orderNum || 0),
52+
),
53+
);
4854
</script>
4955

5056
<style scoped></style>

src/layout/menu/menu.vue

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,10 @@
3535
const currentRoute = useRoute();
3636
const router = useRouter();
3737
38-
const menus = computed(() => userStore.menus);
38+
const menus = computed(() =>
39+
[...userStore.menus].sort((a, b) => (a?.meta?.orderNum || 0) - (b?.meta?.orderNum || 0)),
40+
);
41+
console.log('menus', menus.value);
3942
4043
// 根据activeMenu获取指定的menu
4144
const getTargetMenuByActiveMenuName = (activeMenu: string) => {

src/router/generator-router.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export function filterAsyncRoute(
2323
return routes
2424
.filter((item) => item.type !== 2 && item.isShow && item.parentId == parentRoute?.id)
2525
.map((item) => {
26-
const { router, viewPath, name, icon, keepalive } = item;
26+
const { router, viewPath, name, icon, orderNum, keepalive } = item;
2727
let fullPath = '';
2828
const pathPrefix = lastNamePath.slice(-1)[0] || '';
2929
if (isUrl(router)) {
@@ -47,6 +47,7 @@ export function filterAsyncRoute(
4747
// name: `${viewPath ? toHump(viewPath) : fullPath}-${item.id}`,
4848
name: fullPath,
4949
meta: {
50+
orderNum,
5051
title: name,
5152
perms: [],
5253
icon: icon,

src/router/staticModules/error.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ const moduleName = 'error';
77
export const notFound: RouteRecordRaw = {
88
path: '/:pathMatch(.*)*',
99
name: 'NotFound',
10+
meta: {
11+
title: 'NotFound',
12+
hideInMenu: true,
13+
hideInTabs: true,
14+
},
1015
redirect: '/error/404',
1116
component: () => import(/* webpackChunkName: "404" */ '@/views/error/404.vue'),
1217
};

src/store/modules/user.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ export const useUserStore = defineStore({
7979
this.userInfo = userInfo;
8080
// 生成路由
8181
const generatorResult = generatorDynamicRouter(menus);
82-
this.menus = generatorResult.menus;
82+
this.menus = generatorResult.menus.filter((item) => !item.meta?.hideInMenu);
8383
wsStore.initSocket();
8484

8585
return { menus, perms, userInfo };

types/vue-router.d.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { type PermissionType } from '@/core/permission/modules/types';
22
import { type LocaleType } from '@/locales/config';
3+
import { type RouteMeta as VRouteMeta } from 'vue-router';
34

45
declare global {
56
type Title18n = {
@@ -8,7 +9,7 @@ declare global {
89
}
910

1011
declare module 'vue-router' {
11-
interface RouteMeta extends Record<string | number | symbol, unknown> {
12+
interface RouteMeta extends VRouteMeta {
1213
/** 标题 */
1314
title: string | Title18n;
1415
/** 当前路由是否需要权限验证 */
@@ -35,6 +36,8 @@ declare module 'vue-router' {
3536
hideInTabs?: boolean;
3637
/** 设置当前路由高亮的菜单项,值为route fullPath或route name,一般用于详情页 */
3738
activeMenu?: string;
39+
/** 菜单排序号 */
40+
orderNum?: number;
3841
isLink?: boolean;
3942
}
4043
}

0 commit comments

Comments
 (0)