24 lines
913 B
TypeScript
24 lines
913 B
TypeScript
import { navigate } from "astro:transitions/client";
|
||
|
||
/** 站内软导航(ClientRouter),避免整页 GET。 */
|
||
export function go(href: string): void {
|
||
void navigate(href);
|
||
}
|
||
|
||
/** 读取当前 URL 查询参数(浏览器侧)。 */
|
||
export function queryParam(name: string): string | null {
|
||
if (typeof window === "undefined") return null;
|
||
return new URLSearchParams(window.location.search).get(name);
|
||
}
|
||
|
||
/** 更新查询参数(不刷新,便于刷新/分享后停留在同一选集)。 */
|
||
export function setQueryParams(params: Record<string, string | null>): void {
|
||
if (typeof window === "undefined") return;
|
||
const url = new URL(window.location.href);
|
||
for (const [key, value] of Object.entries(params)) {
|
||
if (value == null) url.searchParams.delete(key);
|
||
else url.searchParams.set(key, value);
|
||
}
|
||
window.history.replaceState(null, "", url.toString());
|
||
}
|