diff --git a/examples/template-vue/src/router/elegant/transform.ts b/examples/template-vue/src/router/elegant/transform.ts index 4e885c2..34114be 100644 --- a/examples/template-vue/src/router/elegant/transform.ts +++ b/examples/template-vue/src/router/elegant/transform.ts @@ -5,7 +5,7 @@ import type { RouteRecordRaw, RouteComponent } from 'vue-router'; import type { ElegantConstRoute } from '@elegant-router/vue'; -import type { RouteKey, RouteMap } from '@elegant-router/types'; +import type { RouteMap, RouteKey, RoutePath } from '@elegant-router/types'; /** * transform elegant const routes to vue routes @@ -163,10 +163,10 @@ export function getRoutePath(name: RouteKey) { * get route name by route path * @param path route path */ -export function getRouteName(path: RouteMap[RouteKey]) { - const routeEntries = Object.entries(routeMap) as [RouteKey, RouteMap[RouteKey]][]; +export function getRouteName(path: RoutePath) { + const routeEntries = Object.entries(routeMap) as [RouteKey, RoutePath][]; - const routeName = routeEntries.find(([, routePath]) => routePath === path)?.[0]; + const routeName: RouteKey | null = routeEntries.find(([, routePath]) => routePath === path)?.[0] || null; - return routeName || null; + return routeName; } diff --git a/examples/template-vue/src/typings/elegant-router.d.ts b/examples/template-vue/src/typings/elegant-router.d.ts index 059b3db..02fe25e 100644 --- a/examples/template-vue/src/typings/elegant-router.d.ts +++ b/examples/template-vue/src/typings/elegant-router.d.ts @@ -38,6 +38,11 @@ declare module "@elegant-router/types" { */ export type RouteKey = keyof RouteMap; + /** + * route path + */ + export type RoutePath = RouteMap[RouteKey]; + /** * custom route key */ diff --git a/packages/vue/src/core/dts.ts b/packages/vue/src/core/dts.ts index facc69b..6bb225d 100644 --- a/packages/vue/src/core/dts.ts +++ b/packages/vue/src/core/dts.ts @@ -76,6 +76,11 @@ declare module "@elegant-router/types" { */ export type RouteKey = keyof RouteMap; + /** + * route path + */ + export type RoutePath = RouteMap[RouteKey]; + /** * custom route key */ diff --git a/packages/vue/src/core/transform.ts b/packages/vue/src/core/transform.ts index 1cf2837..e6dc2e0 100644 --- a/packages/vue/src/core/transform.ts +++ b/packages/vue/src/core/transform.ts @@ -31,7 +31,7 @@ function getTransformCode(options: ElegantVueRouterOption, entries: ElegantRoute import type { RouteRecordRaw, RouteComponent } from 'vue-router'; import type { ElegantConstRoute } from '@elegant-router/vue'; -import type { RouteKey, RouteMap } from '@elegant-router/types'; +import type { RouteMap, RouteKey, RoutePath } from '@elegant-router/types'; /** * transform elegant const routes to vue routes @@ -158,6 +158,9 @@ function transformElegantRouteToVueRoute( return vueRoutes; } +/** + * map of route name and route path + */ const routeMap: RouteMap = { ${allEntries.map(([routeName, routePath]) => `"${routeName}": "${routePath}"`).join(',\n ')} }; @@ -174,12 +177,12 @@ export function getRoutePath(name: RouteKey) { * get route name by route path * @param path route path */ -export function getRouteName(path: RouteMap[RouteKey]) { - const routeEntries = Object.entries(routeMap) as [RouteKey, RouteMap[RouteKey]][]; +export function getRouteName(path: RoutePath) { + const routeEntries = Object.entries(routeMap) as [RouteKey, RoutePath][]; - const routeName = routeEntries.find(([, routePath]) => routePath === path)?.[0]; + const routeName: RouteKey | null = routeEntries.find(([, routePath]) => routePath === path)?.[0] || null; - return routeName || null; + return routeName; } `;