From 17ec8c6af4f186992f843d3c204b80479cab5cc2 Mon Sep 17 00:00:00 2001 From: xiaoxian521 <1923740402@qq.com> Date: Wed, 7 Dec 2022 17:26:40 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E5=8A=A8=E6=80=81?= =?UTF-8?q?=E8=B7=AF=E7=94=B1=20`rank`=20=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/directives/auth/index.ts | 2 +- src/router/utils.ts | 13 +++++++++---- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/directives/auth/index.ts b/src/directives/auth/index.ts index 69118d6..627ea89 100644 --- a/src/directives/auth/index.ts +++ b/src/directives/auth/index.ts @@ -5,7 +5,7 @@ export const auth: Directive = { mounted(el: HTMLElement, binding: DirectiveBinding) { const { value } = binding; if (value) { - !hasAuth(value) && el.parentNode.removeChild(el); + !hasAuth(value) && el.parentNode?.removeChild(el); } else { throw new Error("need auths! Like v-auth=\"['btn.add','btn.edit']\""); } diff --git a/src/router/utils.ts b/src/router/utils.ts index 3510d59..df8d813 100644 --- a/src/router/utils.ts +++ b/src/router/utils.ts @@ -29,9 +29,13 @@ const modulesRoutes = import.meta.glob("/src/views/**/*.{vue,tsx}"); // 动态路由 import { getAsyncRoutes } from "@/api/routes"; -function handRank(ramk: number, name: string, path: string) { - return isAllEmpty(ramk) || (ramk === 0 && name !== "Home" && path !== "/") - ? true +function handRank(routeInfo: any) { + const { name, path, parentId, meta } = routeInfo; + return isAllEmpty(parentId) + ? isAllEmpty(meta?.rank) || + (meta?.rank === 0 && name !== "Home" && path !== "/") + ? true + : false : false; } @@ -39,7 +43,7 @@ function handRank(ramk: number, name: string, path: string) { function ascending(arr: any[]) { arr.forEach((v, index) => { // 当rank不存在时,根据顺序自动创建,首页路由永远在第一位 - if (handRank(v?.meta?.rank, v.name, v.path)) v.meta.rank = index + 2; + if (handRank(v)) v.meta.rank = index + 2; }); return arr.sort( (a: { meta: { rank: number } }, b: { meta: { rank: number } }) => { @@ -351,6 +355,7 @@ function hasAuth(value: string | Array): boolean { if (!value) return false; /** 从当前路由的`meta`字段里获取按钮级别的所有自定义`code`值 */ const metaAuths = getAuths(); + if (!metaAuths) return false; const isAuths = isString(value) ? metaAuths.includes(value) : isIncludeAllChildren(value, metaAuths);