UNPKG

press-next

Version:

Vue3 组件库,支持 Composition API

79 lines (65 loc) 1.73 kB
// #ifdef H5 import { navigateData } from 'press-plus/common/page-animation/index'; // #endif export interface IOptions { home?: string; previousPath?: string | (() => string); isHomeTabBar?: boolean; } 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: '', isHomeTabBar: false, }) { if (getCurrentPages().length === 1) { if (redirectCustomPrePath(options)) { return; } // #ifdef H5 navigateData.setNavType('back'); // #endif if (options.isHomeTabBar) { uni.switchTab({ url: options.home || '/', }); } else { uni.redirectTo({ url: `${options?.home}?disableSetNavType=1`, disableSetNavType: true, }); } } else { uni.navigateBack({ delta: 1 }); } }