Files
rating/utils/page.js

42 lines
1.0 KiB
JavaScript
Raw Normal View History

2026-05-20 15:58:04 +08:00
/**
* 页面跳转相关工具方法
*/
/**
* 智能返回方法默认返回上一页如果页面栈只有1层没有上一页则返回到首页
*
* @param {Object} options 配置参数
* @param {number} options.delay 延迟执行的时间单位毫秒默认 0
* @param {string} options.homePath 首页的路径默认为 '/pages/index/index'
*/
export const smartNavigateBack = (options = {}) => {
const { delay = 0, homePath = '/pages/index/index' } = options;
const executeBack = () => {
const pages = getCurrentPages();
if (pages.length > 1) {
// 有上一页,直接返回
uni.navigateBack({
delta: 1
});
} else {
// 页面栈只有一层,返回首页
uni.switchTab({
url: homePath,
fail: () => {
// 如果首页不是 tabBar 页面,则使用 reLaunch
uni.reLaunch({
url: homePath
});
}
});
}
};
if (delay > 0) {
setTimeout(executeBack, delay);
} else {
executeBack();
}
};