1
This commit is contained in:
@@ -10,3 +10,4 @@ export * from './routeUtil';
|
||||
export * from './share'
|
||||
export * from './genPoster'
|
||||
export * from './wx_helper'
|
||||
export * from './navigation';
|
||||
|
||||
115
src/utils/navigation.ts
Normal file
115
src/utils/navigation.ts
Normal file
@@ -0,0 +1,115 @@
|
||||
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);
|
||||
},
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user