问题描述:快速点击跳转页面后会闪退到登陆页面
解决方案:重新封装uniapp跳转api,加防抖锁,To.ts
import { NavigateToOptions, RedirectToOptions, ReLaunchOptions, SwitchTabOptions } from 'uni-app';
interface ToImplements {
navigateTo(obj: NavigateToOptions): void;
redirectTo(obj: RedirectToOptions): void;
reLaunch(obj: ReLaunchOptions): void;
switchTab(obj: SwitchTabOptions): void;
}
class ToClass implements ToImplements {
private static instance: ToClass | null = null;
private time: number;
private lock: boolean;
private constructor(time: number = 500) {
this.time = time;
this.lock = false;
}
// 单例模式,确保只有一个实例
public static getInstance(time?: number): ToClass {
if (!ToClass.instance) {
ToClass.instance = new ToClass(time);
}
return ToClass.instance;
}
// 私有方法,用于执行导航操作并设置锁
private navigateAndLock<T extends (...args: any[]) => void>(func: T, obj: Parameters<T>[0]): void {
if (this.lock) return;
this.lock = true;
func(obj);
setTimeout(() => {
this.lock = false;
}, this.time);
}
// 保留当前页面,跳转到应用内的某个页面,使用uni.navigateBack可以返回到原页面
navigateTo(obj: NavigateToOptions): void {
this.navigateAndLock(uni.navigateTo, obj);
}
// 关闭当前页面,跳转到应用内的某个页面。但是不允许跳转到 tabbar 页面
redirectTo(obj: RedirectToOptions): void {
this.navigateAndLock(uni.redirectTo, obj);
}
// 关闭所有页面,打开应用内的某个页面
reLaunch(obj: ReLaunchOptions): void {
this.navigateAndLock(uni.reLaunch, obj);
}
// 跳转到 tabBar 页面,并关闭其他所有非 tabBar 页面
switchTab(obj: SwitchTabOptions): void {
this.navigateAndLock(uni.switchTab, obj);
}
}
// 获取单例实例
export const useUni = ToClass.getInstance();
使用方法
import { useUni } from '@/hooks/To'
* useUni.navigateTo({*
url: `/pages/admin/components/details?id=` + id,
})