Skip to content

Commit

Permalink
feat(packages): perfect routing method
Browse files Browse the repository at this point in the history
  • Loading branch information
mufeng889 committed Aug 19, 2024
1 parent a2dde59 commit 7a15b1b
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 9 deletions.
45 changes: 38 additions & 7 deletions packages/simple-router/src/router.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ class CreateRouter {
});

this.reactRouter.getBlocker('beforeGuard', (arg: Parameters<BlockerFunction>[0]) =>
this.onBeforeRouteChange(arg, beforeEach, firstInit)
this.#onBeforeRouteChange(arg, beforeEach, firstInit)
);

this.reactRouter.subscribe((state: RouterState) => this.afterRouteChange(state, afterEach));
this.reactRouter.subscribe((state: RouterState) => this.#afterRouteChange(state, afterEach));
this.initReactRoutes = initReactRoutes;
this.getReactRoutes = getReactRoutes;
Promise.resolve().then(async () => {
Expand Down Expand Up @@ -91,7 +91,7 @@ class CreateRouter {
this.reactRouter._internalSetRoutes([...this.initReactRoutes, ...this.reactRoutes]);
}

onBeforeRouteChange = (
#onBeforeRouteChange = (
{ currentLocation, nextLocation }: Parameters<BlockerFunction>[0],
beforeEach: RouterOptions['beforeEach'],
firstInit: (allNames: string[]) => void
Expand All @@ -111,7 +111,14 @@ class CreateRouter {

const to = this.resolve(nextLocation);

if (to.matched[0]?.redirect === this.currentRoute.path) return true;
const matchedRoutes = to.matched;
const nextRoute = matchedRoutes[matchedRoutes.length - 1];

const finalPath = getFullPath(nextRoute);

if (finalPath === this.currentRoute.path || matchedRoutes[0]?.redirect === this.currentRoute.path) {
return true;
}

const next = (param?: boolean | string) => {
if (!param) return false;
Expand Down Expand Up @@ -211,7 +218,7 @@ class CreateRouter {
return this.getRoutes().find(route => route.name === key)?.meta || null;
}

afterRouteChange = (state: RouterState, afterEach: RouterOptions['afterEach']) => {
#afterRouteChange = (state: RouterState, afterEach: RouterOptions['afterEach']) => {
if (state.navigation.state === 'idle') {
const from = this.currentRoute;
this.currentRoute = this.resolve(state.location);
Expand Down Expand Up @@ -241,13 +248,25 @@ class CreateRouter {
return this.matcher.getRecordMatcher(name)?.record;
}

push(to: RouteLocationNamedRaw | string) {
push(to: RouteLocationNamedRaw | string | Location, replace?: true) {
const target = typeof to === 'string' ? to : this.resolve(to).fullPath;

if (target !== this.currentRoute.fullPath) {
this.reactRouter.navigate(target);
this.reactRouter.navigate(target, { replace });
}
}

back() {
this.go(-1);
}

forwardRef() {
this.go(1);
}

go(delta: number) {
this.reactRouter.navigate(delta);
}
}

export default CreateRouter;
Expand All @@ -256,3 +275,15 @@ function cleanParams(params: Record<string, any> | undefined): Record<string, an
if (!params) return {};
return Object.fromEntries(Object.entries(params).filter(([_, value]) => value !== null));
}

function getFullPath(route: RouteRecordNormalized | ElegantConstRoute): string {
// 如果当前 route 存在并且有 children
if (route && route.children && route.children.length > 0) {
// 获取第一个子路由
const firstChild = route.children[0];
// 递归调用,继续拼接子路由的 path
return `${route.path}/${getFullPath(firstChild)}`;
}
// 如果没有 children,返回当前 route 的 path
return route.path;
}
6 changes: 4 additions & 2 deletions src/router/elegant/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ export function transformElegantRouteToReactRoute(
}





if (children?.length) {
Expand All @@ -182,11 +182,13 @@ export function transformElegantRouteToReactRoute(
}else if (redirectTo) {
reactRoute.loader=()=>redirect(redirectTo)
}

if (loader) {
reactRoute.loader = () => loader
}



if (layout) {

return {
Expand Down

0 comments on commit 7a15b1b

Please sign in to comment.