mirror of
https://github.com/m-xlsea/ruoyi-plus-soybean.git
synced 2025-09-24 07:49:47 +08:00
refactor(projects): 优化路由声明,添加路由模块导入,规范路相关文件夹
This commit is contained in:
@ -1,3 +1,4 @@
|
||||
import type { Component } from 'vue';
|
||||
import type { RouteRecordRaw } from 'vue-router';
|
||||
|
||||
function getCacheName(route: RouteRecordRaw, isCache: boolean) {
|
||||
@ -5,6 +6,7 @@ function getCacheName(route: RouteRecordRaw, isCache: boolean) {
|
||||
const hasChild = hasChildren(route);
|
||||
if (isCache && !hasChild) {
|
||||
const name = route.name as string;
|
||||
setComponentName(route.component, name);
|
||||
cacheNames.push(name);
|
||||
}
|
||||
if (hasChild) {
|
||||
@ -24,6 +26,13 @@ function hasChildren(route: RouteRecordRaw) {
|
||||
return Boolean(route.children && route.children.length);
|
||||
}
|
||||
|
||||
/** 给需要缓存的页面组件设置名称 */
|
||||
export function setComponentName(component?: Component, name?: string) {
|
||||
if (component && name) {
|
||||
Object.assign(component, { name });
|
||||
}
|
||||
}
|
||||
|
||||
/** 获取被缓存的路由 */
|
||||
export function getCacheRoutes(routes: RouteRecordRaw[]) {
|
||||
const cacheNames: string[] = [];
|
||||
|
@ -1,34 +1,74 @@
|
||||
import type { Component } from 'vue';
|
||||
import type { CustomRoute } from '@/interface';
|
||||
import { router } from '@/router';
|
||||
import type { Router } from 'vue-router';
|
||||
import type { CustomRoute, ImportedRouteModules, CustomRouteMeta } from '@/interface';
|
||||
|
||||
/** 给需要缓存的页面组件设置名称 */
|
||||
export function setRouterCacheName(component: Component, name?: string) {
|
||||
if (name) {
|
||||
Object.assign(component, { name });
|
||||
/** 处理导入的路由模块 */
|
||||
export function transformRouteModules(routeModules: ImportedRouteModules) {
|
||||
const modules = Object.keys(routeModules).map(key => {
|
||||
return routeModules[key].default;
|
||||
});
|
||||
const constantRoutes: CustomRoute[] = modules.sort((next, pre) => Number(next.meta.order) - Number(pre.meta.order));
|
||||
return constantRoutes;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取路由的首页
|
||||
* @param routes - 路由
|
||||
* @param routeHomeName - 路由首页名称
|
||||
*/
|
||||
export function getRouteHome(routes: CustomRoute[], routeHomeName: string) {
|
||||
let routeHome: CustomRoute;
|
||||
function hasChildren(route: CustomRoute) {
|
||||
return Boolean(route.children && route.children.length);
|
||||
}
|
||||
function getRouteHomeByRoute(route: CustomRoute) {
|
||||
if (routeHome) return;
|
||||
const hasChild = hasChildren(route);
|
||||
if (!hasChild) {
|
||||
if (route.name === routeHomeName) {
|
||||
routeHome = route;
|
||||
}
|
||||
} else {
|
||||
getRouteHomeByRoutes(route.children as CustomRoute[]);
|
||||
}
|
||||
}
|
||||
function getRouteHomeByRoutes(_routes: CustomRoute[]) {
|
||||
_routes.some(item => {
|
||||
getRouteHomeByRoute(item as CustomRoute);
|
||||
return routeHome !== undefined;
|
||||
});
|
||||
}
|
||||
getRouteHomeByRoutes(routes);
|
||||
return routeHome!;
|
||||
}
|
||||
|
||||
/** 获取登录后的重定向地址 */
|
||||
export function getLoginRedirectUrl() {
|
||||
export function getLoginRedirectUrl(router: Router) {
|
||||
const path = router.currentRoute.value.fullPath as string;
|
||||
const redirectUrl = path === '/' ? undefined : path;
|
||||
return redirectUrl;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置单个路由
|
||||
* @param route - 路由
|
||||
* @param notFoundName - 404未找到的路由名称
|
||||
* @param container - 路由容器
|
||||
*/
|
||||
export function setSingleRoute(container: Component, route: CustomRoute, notFoundName = 'not-found') {
|
||||
interface SingleRouteConfig {
|
||||
/** 路由 */
|
||||
route: CustomRoute;
|
||||
/** 路由容器 */
|
||||
container: Component;
|
||||
/** 路由容器的描述 */
|
||||
meta: CustomRouteMeta;
|
||||
/** 404路由的名称 */
|
||||
notFoundName: string;
|
||||
}
|
||||
/** * 设置单个路由 */
|
||||
export function setSingleRoute(config: SingleRouteConfig) {
|
||||
const { route, container, meta, notFoundName } = config;
|
||||
const routeItem: CustomRoute = {
|
||||
name: `${route.name as string}_`,
|
||||
path: `${route.path}_`,
|
||||
component: container,
|
||||
redirect: { name: notFoundName },
|
||||
meta: {
|
||||
...meta,
|
||||
isNotMenu: true
|
||||
},
|
||||
children: [route]
|
||||
|
@ -1,5 +1,5 @@
|
||||
import type { CustomRoute, GlobalMenuOption } from '@/interface';
|
||||
import { iconifyRender } from '@/utils';
|
||||
import { iconifyRender } from '../common';
|
||||
|
||||
/** 判断路由是否作为菜单 */
|
||||
function asMenu(route: CustomRoute) {
|
||||
|
Reference in New Issue
Block a user