Browse Source

fix: 修复动态路由 `rank` 问题

i18n
xiaoxian521 2 years ago
parent
commit
17ec8c6af4
  1. 2
      src/directives/auth/index.ts
  2. 11
      src/router/utils.ts

2
src/directives/auth/index.ts

@ -5,7 +5,7 @@ export const auth: Directive = {
mounted(el: HTMLElement, binding: DirectiveBinding) { mounted(el: HTMLElement, binding: DirectiveBinding) {
const { value } = binding; const { value } = binding;
if (value) { if (value) {
!hasAuth(value) && el.parentNode.removeChild(el);
!hasAuth(value) && el.parentNode?.removeChild(el);
} else { } else {
throw new Error("need auths! Like v-auth=\"['btn.add','btn.edit']\""); throw new Error("need auths! Like v-auth=\"['btn.add','btn.edit']\"");
} }

11
src/router/utils.ts

@ -29,9 +29,13 @@ const modulesRoutes = import.meta.glob("/src/views/**/*.{vue,tsx}");
// 动态路由 // 动态路由
import { getAsyncRoutes } from "@/api/routes"; import { getAsyncRoutes } from "@/api/routes";
function handRank(ramk: number, name: string, path: string) {
return isAllEmpty(ramk) || (ramk === 0 && name !== "Home" && path !== "/")
function handRank(routeInfo: any) {
const { name, path, parentId, meta } = routeInfo;
return isAllEmpty(parentId)
? isAllEmpty(meta?.rank) ||
(meta?.rank === 0 && name !== "Home" && path !== "/")
? true ? true
: false
: false; : false;
} }
@ -39,7 +43,7 @@ function handRank(ramk: number, name: string, path: string) {
function ascending(arr: any[]) { function ascending(arr: any[]) {
arr.forEach((v, index) => { arr.forEach((v, index) => {
// 当rank不存在时,根据顺序自动创建,首页路由永远在第一位 // 当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( return arr.sort(
(a: { meta: { rank: number } }, b: { meta: { rank: number } }) => { (a: { meta: { rank: number } }, b: { meta: { rank: number } }) => {
@ -351,6 +355,7 @@ function hasAuth(value: string | Array<string>): boolean {
if (!value) return false; if (!value) return false;
/** 从当前路由的`meta`字段里获取按钮级别的所有自定义`code`值 */ /** 从当前路由的`meta`字段里获取按钮级别的所有自定义`code`值 */
const metaAuths = getAuths(); const metaAuths = getAuths();
if (!metaAuths) return false;
const isAuths = isString(value) const isAuths = isString(value)
? metaAuths.includes(value) ? metaAuths.includes(value)
: isIncludeAllChildren(value, metaAuths); : isIncludeAllChildren(value, metaAuths);

Loading…
Cancel
Save