add history

This commit is contained in:
2025-03-29 17:48:38 +08:00
parent 415f008209
commit 69784e8ed4
3 changed files with 62 additions and 5 deletions

View File

@@ -199,3 +199,31 @@ export class Page {
this.popstate({ state } as any, { type: 'all' });
}
}
/**
* 获取history state
* @returns
*/
export const getHistoryState = <T = Record<string, any>>() => {
const history = window.history;
const state = history.state || {};
return state as T;
};
/**
* 设置history state
* @param state
*/
export const setHistoryState = (state: any) => {
const history = window.history;
const oldState = getHistoryState();
// 只更新 state 而不改变 URL
history.replaceState({ ...oldState, ...state }, '', window.location.href);
};
/**
* 清除history state
*/
export const clearHistoryState = () => {
const history = window.history;
history.replaceState({}, '', window.location.href);
};