Skip to content

Commit

Permalink
fix(packages): support pass state and fix judgments before jumpe
Browse files Browse the repository at this point in the history
  • Loading branch information
mufeng889 committed Sep 8, 2024
1 parent 1dad4f0 commit 3493583
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 8 deletions.
1 change: 1 addition & 0 deletions packages/simple-router/src/matcher/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ class CreateRouterMatcher {
}
return {
fullPath,
state: location.state,
name,
path,
hash: location.hash,
Expand Down
2 changes: 1 addition & 1 deletion packages/simple-router/src/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ export function stringifyQuery(query: LocationQueryRaw): string {
export function decode(text: string | number): string {
try {
return decodeURIComponent(`${text}`);
} catch (err) {
} catch {
console.warn(`Error decoding "${text}". Using original value`);
}
return `${text}`;
Expand Down
15 changes: 9 additions & 6 deletions packages/simple-router/src/router.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,13 @@ class CreateRouter {
}

#onBeforeRouteChange = (
{ currentLocation, nextLocation }: Parameters<BlockerFunction>[0],
{ nextLocation }: Parameters<BlockerFunction>[0],
beforeEach: RouterOptions['beforeEach'],
firstInit: (allNames: string[]) => void
) => {
if (nextLocation.pathname === currentLocation.pathname && this.initRoute) {
const to = this.resolve(nextLocation);

if (to.fullPath === this.currentRoute.fullPath && this.initRoute) {
return true;
}

Expand All @@ -147,8 +149,6 @@ class CreateRouter {
this.initRoute = true;
}

const to = this.resolve(nextLocation);

if (to.redirect) {
if (to.redirect.startsWith('/')) {
if (to.redirect === this.currentRoute.fullPath) {
Expand Down Expand Up @@ -299,10 +299,13 @@ class CreateRouter {
}

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

const state = resolved.state || null;

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

Expand Down
2 changes: 1 addition & 1 deletion packages/simple-router/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ export interface Router {
go: (delta: number) => void;
removeRoute: (name: string) => void;
}
export interface HistoryStateArray extends Array<HistoryStateValue> {}
export type HistoryStateArray = Array<HistoryStateValue>;

export type HistoryStateValue = string | number | boolean | null | undefined | HistoryState | HistoryStateArray;

Expand Down

0 comments on commit 3493583

Please sign in to comment.