staff/hooks/useNavigation.ts

49 lines
1.2 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { ref } from 'vue'
/**
* 导航栏状态管理 Composable
* 检查当前路由栈是否大于1条用于决定是否显示返回首页按钮
* @returns 包含路由栈状态和相关方法的对象
*/
export function useNavigation() {
// 是否有多个页面在路由栈中
const hasMultiplePages = ref(false)
// 当前页面是否为 tabBar 页面
const isTabBarPage = ref(false)
// tabBar 页面路径列表
const tabBarPages = [
'pages/index/index',
'pages/message/index',
'pages/mine/index'
]
/**
* 检查当前页面是否为 tabBar 页面
*/
const checkIsTabBarPage = (currentRoute: string) => {
isTabBarPage.value = tabBarPages.includes(currentRoute)
}
/**
* 检查当前路由栈状态
* 在每个页面的 onShow 或 onLoad 生命周期调用此方法
*/
const checkRouteStack = () => {
// 获取当前页面路由
const pages = getCurrentPages()
// 获取当前页面路径
const currentRoute = pages[pages.length - 1]?.route || ''
// 更新路由栈状态
hasMultiplePages.value = pages.length > 1
// 检查是否为 tabBar 页面
checkIsTabBarPage(currentRoute)
}
return {
hasMultiplePages,
isTabBarPage,
checkRouteStack
}
}