feat: add listen code change

This commit is contained in:
2024-09-23 20:51:59 +08:00
parent 4d308921a3
commit 27f487c48f
13 changed files with 419 additions and 97 deletions

22
src/utils/history.ts Normal file
View File

@@ -0,0 +1,22 @@
type To = string | Location;
type State<T> = {
[key: string]: any;
} & T;
export const push = <T = any>(to: To, state?: State<T>, refresh = true) => {
const _history = window.history;
if (typeof to === 'string') {
// must key is default, so react navigate can work
_history.pushState({ key: 'default', usr: state }, '', to);
} else {
// const path = to.pathname;
_history.pushState({ key: 'default', usr: state }, '', to.pathname);
}
// must dispatch popstate event, so react navigate can work
refresh && window.dispatchEvent(new Event('popstate'));
};
export const history = {
push,
};
// import { createBrowserHistory } from 'history';
// export const history = createBrowserHistory();