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);