refactor: 优化路由组件解析逻辑,更新字典数据注释及注册组件样式

This commit is contained in:
Ray.Hao
2026-01-09 23:08:53 +08:00
parent 6d88b5ee07
commit ca15fa0322
4 changed files with 19 additions and 9 deletions

View File

@@ -8,6 +8,18 @@ import { RouteItem } from "@/types";
const modules = import.meta.glob("../../views/**/**.vue");
const Layout = () => import("../../layouts/index.vue");
function resolveViewComponent(componentPath: string) {
const normalized = componentPath
.trim()
.replace(/^\/+/, "")
.replace(/\.vue$/i, "");
return (
modules[`../../views/${normalized}.vue`] ||
modules[`../../views/${normalized}/index.vue`] ||
modules[`../../views/error/404.vue`]
);
}
export const usePermissionStore = defineStore("permission", () => {
// 所有路由(静态路由 + 动态路由)
const routes = ref<RouteRecordRaw[]>([]);
@@ -84,10 +96,7 @@ const transformRoutes = (routes: RouteItem[], isTopLevel: boolean = true): Route
} else {
// 动态导入组件Layout特殊处理找不到组件时返回404
normalizedRoute.component =
processedComponent === "Layout"
? Layout
: modules[`../../views/${processedComponent}.vue`] ||
modules[`../../views/error/404.vue`];
processedComponent === "Layout" ? Layout : resolveViewComponent(processedComponent);
}
// 递归处理子路由