feat:对返回上一页做了处理
This commit is contained in:
52
nkebao/src/utils/navigation.ts
Normal file
52
nkebao/src/utils/navigation.ts
Normal file
@@ -0,0 +1,52 @@
|
||||
import { NavigateFunction } from 'react-router-dom';
|
||||
|
||||
/**
|
||||
* 智能返回上一页
|
||||
* 如果没有上一页历史记录,则返回到主页
|
||||
* @param navigate React Router的navigate函数
|
||||
* @param fallbackPath 默认返回路径,默认为'/'
|
||||
*/
|
||||
export const smartGoBack = (navigate: NavigateFunction, fallbackPath: string = '/') => {
|
||||
// 检查是否有历史记录
|
||||
if (window.history.length > 1) {
|
||||
// 尝试返回上一页
|
||||
navigate(-1);
|
||||
} else {
|
||||
// 如果没有历史记录,返回到主页
|
||||
navigate(fallbackPath);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* 带确认的智能返回
|
||||
* @param navigate React Router的navigate函数
|
||||
* @param message 确认消息
|
||||
* @param fallbackPath 默认返回路径
|
||||
*/
|
||||
export const smartGoBackWithConfirm = (
|
||||
navigate: NavigateFunction,
|
||||
message: string = '确定要返回吗?未保存的内容将丢失。',
|
||||
fallbackPath: string = '/'
|
||||
) => {
|
||||
if (window.confirm(message)) {
|
||||
smartGoBack(navigate, fallbackPath);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* 检查是否可以返回上一页
|
||||
* @returns boolean
|
||||
*/
|
||||
export const canGoBack = (): boolean => {
|
||||
return window.history.length > 1;
|
||||
};
|
||||
|
||||
/**
|
||||
* 获取返回按钮文本
|
||||
* @param hasHistory 是否有历史记录
|
||||
* @returns 按钮文本
|
||||
*/
|
||||
export const getBackButtonText = (hasHistory?: boolean): string => {
|
||||
const canBack = hasHistory !== undefined ? hasHistory : canGoBack();
|
||||
return canBack ? '返回上一页' : '返回主页';
|
||||
};
|
||||
Reference in New Issue
Block a user