From 4b9c1e322e1dca5aee73c4738434a76a3a1ddfab Mon Sep 17 00:00:00 2001 From: wang <1509326266@qq.com> Date: Tue, 30 Jul 2024 00:35:19 +0800 Subject: [PATCH] fix(projects): dts and transform supports multilevel routing user-defined jump subpages --- src/route-core/dts.ts | 3 ++- src/route-core/transform.ts | 22 +++++++++++----------- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/src/route-core/dts.ts b/src/route-core/dts.ts index 4d55c47..d90d3c0 100644 --- a/src/route-core/dts.ts +++ b/src/route-core/dts.ts @@ -198,6 +198,7 @@ declare module "@elegant-router/types" { name: K; path: RouteMap[K]; component: \`${LAYOUT_PREFIX}\${RouteLayout}$${VIEW_PREFIX}\${K}\`| \`${VIEW_PREFIX}\${LastLevelRouteKey}\`; + children?:ElegantConstRoute[] ; layout?:${layoutType} } : never; @@ -244,7 +245,7 @@ declare module "@elegant-router/types" { ? Omit & { name: K; path: RouteMap[K]; - component?: \`${LAYOUT_PREFIX}\${RouteLayout}$${VIEW_PREFIX}\${LastLevelRouteKey}\`| \`${VIEW_PREFIX}\${LastLevelRouteKey}\`; + component?: \`${LAYOUT_PREFIX}\${RouteLayout}$${VIEW_PREFIX}\${LastLevelRouteKey}\`| \`${VIEW_PREFIX}\${LastLevelRouteKey}\`| \`$${VIEW_PREFIX}\${LastLevelRouteKey}\`; layout?:${layoutType} } : never; diff --git a/src/route-core/transform.ts b/src/route-core/transform.ts index ef3ba73..e689fca 100644 --- a/src/route-core/transform.ts +++ b/src/route-core/transform.ts @@ -32,7 +32,7 @@ function getTransformCode(options: ElegantReactRouterOption, entries: ElegantRou import type { LazyRouteFunction, RouteObject,IndexRouteObject } from "react-router-dom"; import type { FunctionComponent } from "react"; -import type { ElegantConstRoute } from @ohh-889/react-auto-route'; +import type { ElegantConstRoute } from '@ohh-889/react-auto-route'; import type { RouteMap, RouteKey, RoutePath } from '@elegant-router/types'; import { redirect } from 'react-router-dom' import ErrorBoundary from '@/pages/_builtin/error' @@ -113,7 +113,7 @@ export function transformElegantRouteToReactRoute( const [layout, view] = component.split(FIRST_LEVEL_ROUTE_COMPONENT_SPLIT); return { - layout: getLayoutName(layout), + layout: layout?getLayoutName(layout):undefined, view: getViewName(view) }; } @@ -190,25 +190,25 @@ export function transformElegantRouteToReactRoute( return {}; } -if (redirectTo) { - reactRoute.loader=()=>redirect(redirectTo) - } - if (loader) { - reactRoute.loader = () => loader - } + if (children?.length) { reactRoute.children = children.flatMap(child => transformElegantRouteToReactRoute(child, layouts, views)); + const defaultRedirectPath = redirectTo || getRedirectPath(path as string, children[0].path as string); - if (!redirectTo) { reactRoute.children.unshift({ index: true, - loader: () => redirect(getRedirectPath(path as string,children[0].path as string) ), + loader: () => redirect(defaultRedirectPath), ...rest }); - } + }else if (redirectTo) { + reactRoute.loader=()=>redirect(redirectTo) + } + + if (loader) { + reactRoute.loader = () => loader } if (layout) {