Skip to content

Commit

Permalink
fix(projects): fix route const
Browse files Browse the repository at this point in the history
  • Loading branch information
honghuangdc committed Sep 1, 2023
1 parent eff0ad1 commit ed96888
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 13 deletions.
4 changes: 3 additions & 1 deletion examples/template-vue/src/router/elegant/imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import type { LastLevelRouteKey, RouteLayout } from "@elegant-router/types";


export const layouts: Record<RouteLayout, RouteComponent | (() => Promise<RouteComponent>)> = {
default: () => import("@/layouts/default-layout/index.vue"),
base: () => import("@/layouts/base-layout/index.vue"),
};

export const views: Record<LastLevelRouteKey, RouteComponent | (() => Promise<RouteComponent>)> = {
Expand All @@ -19,4 +19,6 @@ export const views: Record<LastLevelRouteKey, RouteComponent | (() => Promise<Ro
"demo-a_child2_child3": () => import("@/views/demo-a/child2/child3/index.vue"),
"demo-a_child3": () => import("@/views/demo-a/child3/index.vue"),
demo3: () => import("@/views/demo3/[id].vue"),
"multi-menu_first_child": () => import("@/views/multi-menu/first_child/index.vue"),
"multi-menu_second_child_home": () => import("@/views/multi-menu/second_child_home/index.vue"),
};
69 changes: 64 additions & 5 deletions examples/template-vue/src/router/elegant/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type { ElegantRoute } from '@elegant-router/types';
export const autoRoutes: ElegantRoute[] = [
{
path: '/403',
component: 'layout.default',
component: 'layout.base',
children: [
{
name: '403',
Expand All @@ -20,7 +20,7 @@ export const autoRoutes: ElegantRoute[] = [
},
{
path: '/404',
component: 'layout.default',
component: 'layout.base',
children: [
{
name: '404',
Expand All @@ -34,7 +34,7 @@ export const autoRoutes: ElegantRoute[] = [
},
{
path: '/500',
component: 'layout.default',
component: 'layout.base',
children: [
{
name: '500',
Expand All @@ -49,7 +49,7 @@ export const autoRoutes: ElegantRoute[] = [
{
name: 'demo-a',
path: '/demo-a',
component: 'layout.default',
component: 'layout.base',
redirect: {
name: 'demo-a_child1'
},
Expand Down Expand Up @@ -95,7 +95,7 @@ export const autoRoutes: ElegantRoute[] = [
},
{
path: '/demo3/:id',
component: 'layout.default',
component: 'layout.base',
children: [
{
name: 'demo3',
Expand All @@ -106,5 +106,64 @@ export const autoRoutes: ElegantRoute[] = [
}
}
]
},
{
name: 'multi-menu',
path: '/multi-menu',
component: 'layout.base',
redirect: {
name: 'multi-menu_first'
},
meta: {
title: 'multi-menu'
},
children: [
{
name: 'multi-menu_first',
path: '/multi-menu/first',
redirect: {
name: 'multi-menu_first_child'
},
meta: {
title: 'multi-menu_first'
}
},
{
name: 'multi-menu_first_child',
path: '/multi-menu/first/child',
component: 'view.multi-menu_first_child',
meta: {
title: 'multi-menu_first_child'
}
},
{
name: 'multi-menu_second',
path: '/multi-menu/second',
redirect: {
name: 'multi-menu_second_child'
},
meta: {
title: 'multi-menu_second'
}
},
{
name: 'multi-menu_second_child',
path: '/multi-menu/second/child',
redirect: {
name: 'multi-menu_second_child_home'
},
meta: {
title: 'multi-menu_second_child'
}
},
{
name: 'multi-menu_second_child_home',
path: '/multi-menu/second/child/home',
component: 'view.multi-menu_second_child_home',
meta: {
title: 'multi-menu_second_child_home'
}
}
]
}
];
11 changes: 10 additions & 1 deletion examples/template-vue/src/typings/elegant-router.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ declare module "@elegant-router/types" {
/**
* route layout
*/
export type RouteLayout = "default";
export type RouteLayout = "base";

/**
* route map
Expand All @@ -26,6 +26,12 @@ declare module "@elegant-router/types" {
"demo-a_child2_child3": "/demo-a/child2/child3";
"demo-a_child3": "/demo-a/child3";
"demo3": "/demo3/:id";
"multi-menu": "/multi-menu";
"multi-menu_first": "/multi-menu/first";
"multi-menu_first_child": "/multi-menu/first/child";
"multi-menu_second": "/multi-menu/second";
"multi-menu_second_child": "/multi-menu/second/child";
"multi-menu_second_child_home": "/multi-menu/second/child/home";
};

/**
Expand Down Expand Up @@ -57,6 +63,7 @@ declare module "@elegant-router/types" {
| "500"
| "demo-a"
| "demo3"
| "multi-menu"
>;

/**
Expand All @@ -71,6 +78,8 @@ declare module "@elegant-router/types" {
| "demo-a_child2_child3"
| "demo-a_child3"
| "demo3"
| "multi-menu_first_child"
| "multi-menu_second_child_home"
>;

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<template>
<div>demo-a-child1</div>
</template>

<script setup lang="ts"></script>

<style scoped></style>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<template>
<div>demo-a-child1</div>
</template>

<script setup lang="ts"></script>

<style scoped></style>
23 changes: 17 additions & 6 deletions packages/vue/src/context/const.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { existsSync } from 'node:fs';
import { writeFile } from 'node:fs/promises';
import { loadFile, generateCode } from 'magicast';
import { parse } from 'recast/parsers/typescript';
import { PAGE_DEGREE_SPLITTER } from '@elegant-router/core';
import type { ElegantRouterTree } from '@elegant-router/core';
import { createPrefixCommentOfGenFile } from './comment';

Expand Down Expand Up @@ -70,19 +69,31 @@ export const autoRoutes: ElegantRoute[] = [];

export function getUpdatedRouteConst(oldConst: AutoRoute[], newConst: AutoRoute[]) {
const updated = newConst.map(item => {
const findItem = oldConst.find(i => (i?.name && i.name === item?.name) || i.path === item.path);
const hasName = Boolean(item?.name);

const findItem = oldConst.find(i => (hasName && i.name === item.name) || i.path === item.path);

if (!findItem) {
return item;
}

const isFirstLevel = !findItem?.name || (findItem.name as string).split(PAGE_DEGREE_SPLITTER).length === 1;
if (hasName) {
findItem.path = item.path;
}

if (findItem.component) {
findItem.component = item.component;
}

if (isFirstLevel) {
return findItem;
if (findItem.redirect) {
findItem.redirect = item.redirect;
}

findItem.children = getUpdatedRouteConst(findItem.children || [], item.children || []);
const children = getUpdatedRouteConst(findItem.children || [], item.children || []);

if (children.length) {
findItem.children = children;
}

return findItem;
});
Expand Down

0 comments on commit ed96888

Please sign in to comment.