enhance(client): improve router

Fix #8902
This commit is contained in:
syuilo
2022-06-29 16:00:00 +09:00
parent 4fd386c3dc
commit bb68cfaa81
4 changed files with 22 additions and 4 deletions

View File

@@ -8,6 +8,7 @@ type RouteDef = {
component: Component;
query?: Record<string, string>;
name?: string;
hash?: string;
globalCacheKey?: string;
};
@@ -78,7 +79,12 @@ export class Router extends EventEmitter<{
public resolve(path: string): { route: RouteDef; props: Map<string, string>; } | null {
let queryString: string | null = null;
let hash: string | null = null;
if (path[0] === '/') path = path.substring(1);
if (path.includes('#')) {
hash = path.substring(path.indexOf('#') + 1);
path = path.substring(0, path.indexOf('#'));
}
if (path.includes('?')) {
queryString = path.substring(path.indexOf('?') + 1);
path = path.substring(0, path.indexOf('?'));
@@ -127,6 +133,10 @@ export class Router extends EventEmitter<{
if (parts.length !== 0) continue forEachRouteLoop;
if (route.hash != null && hash != null) {
props.set(route.hash, hash);
}
if (route.query != null && queryString != null) {
const queryObject = [...new URLSearchParams(queryString).entries()]
.reduce((obj, entry) => ({ ...obj, [entry[0]]: entry[1] }), {});
@@ -138,6 +148,7 @@ export class Router extends EventEmitter<{
}
}
}
return {
route,
props,