press-next
Version:
Vue3 组件库,支持 Composition API
71 lines (57 loc) • 1.56 kB
text/typescript
// #ifdef H5
import { navigateData } from 'press-plus/common/page-animation/index';
// #endif
export interface IOptions {
home?: string;
previousPath?: string | (() => string);
}
function redirectCustomPrePath(options: IOptions) {
const pages = getCurrentPages();
if (!pages.length) return false;
const curPage = pages[pages.length - 1];
let path;
if (options?.previousPath) {
path = typeof options.previousPath === 'function' ? options.previousPath() : options.previousPath;
} else if (process.env.UNI_PLATFORM === 'h5') {
path = (curPage as any)?.getNavigatorPreviousPath?.();
} else {
path = curPage?.$vm?.getNavigatorPreviousPath?.();
}
if (path) {
if (path === '/') {
// #ifdef H5
navigateData.setNavType('back');
// #endif
uni.redirectTo({
url: `${options?.home}?disableSetNavType=1`,
});
return true;
}
// #ifdef H5
navigateData.setNavType('back');
// #endif
uni.redirectTo({
url: path.includes('?') ? `${path}&disableSetNavType=1` : `${path}?disableSetNavType=1`,
});
return true;
}
}
export function goBack(options: IOptions = {
home: '/views/index/index',
previousPath: '',
}) {
if (getCurrentPages().length === 1) {
if (redirectCustomPrePath(options)) {
return;
}
// #ifdef H5
navigateData.setNavType('back');
// #endif
uni.redirectTo({
url: `${options?.home}?disableSetNavType=1`,
disableSetNavType: true,
});
} else {
uni.navigateBack({ delta: 1 });
}
}