Files
mini-programs/src/utils/navigation.ts
张成 0adab95d34 1
2025-11-14 22:54:36 +08:00

116 lines
2.7 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import Taro from "@tarojs/taro";
/**
* 导航工具函数 - 封装页面跳转,提供流畅的过渡效果
*/
interface NavigateOptions {
url: string;
success?: (res: any) => void;
fail?: (err: any) => void;
complete?: (res: any) => void;
}
/**
* 导航到新页面(带过渡动画)
* 使用 Taro.navigateTo系统会自动提供过渡动画
*/
export const navigateTo = (options: NavigateOptions): Promise<TaroGeneral.NavigateToSuccessCallbackResult> => {
return new Promise((resolve, reject) => {
(Taro as any).navigateTo({
...options,
success: (res: any) => {
options.success?.(res);
resolve(res);
},
fail: (err: any) => {
options.fail?.(err);
reject(err);
},
complete: (res: any) => {
options.complete?.(res);
},
});
});
};
/**
* 重定向到新页面(带过渡动画)
* 使用 Taro.redirectTo系统会自动提供过渡动画
*/
export const redirectTo = (options: NavigateOptions): Promise<TaroGeneral.NavigateToSuccessCallbackResult> => {
return new Promise((resolve, reject) => {
(Taro as any).redirectTo({
...options,
success: (res: any) => {
options.success?.(res);
resolve(res);
},
fail: (err: any) => {
options.fail?.(err);
reject(err);
},
complete: (res: any) => {
options.complete?.(res);
},
});
});
};
/**
* 返回上一页(带过渡动画)
*/
export const navigateBack = (options?: { delta?: number }): Promise<TaroGeneral.NavigateBackSuccessCallbackResult> => {
return new Promise((resolve, reject) => {
(Taro as any).navigateBack({
delta: options?.delta || 1,
success: (res: any) => {
resolve(res);
},
fail: (err: any) => {
reject(err);
},
});
});
};
/**
* 切换到 tabBar 页面(无过渡动画,这是 tabBar 的特性)
*/
export const switchTab = (options: { url: string }): Promise<TaroGeneral.SwitchTabSuccessCallbackResult> => {
return new Promise((resolve, reject) => {
Taro.switchTab({
...options,
success: (res: any) => {
resolve(res);
},
fail: (err: any) => {
reject(err);
},
});
});
};
/**
* 重新加载当前页面
*/
export const reLaunch = (options: NavigateOptions): Promise<TaroGeneral.ReLaunchSuccessCallbackResult> => {
return new Promise((resolve, reject) => {
(Taro as any).reLaunch({
...options,
success: (res: any) => {
options.success?.(res);
resolve(res);
},
fail: (err: any) => {
options.fail?.(err);
reject(err);
},
complete: (res: any) => {
options.complete?.(res);
},
});
});
};