2022-11-18 18:44:20 +00:00
|
|
|
import { RouterKey } from '@/symbols'
|
|
|
|
import { requireInjection } from '@/utils'
|
2022-11-19 18:04:21 +00:00
|
|
|
import Router from '@/router'
|
|
|
|
|
|
|
|
let router: Router
|
2022-11-18 18:44:20 +00:00
|
|
|
|
|
|
|
export const useRouter = () => {
|
2022-11-19 18:04:21 +00:00
|
|
|
router = router || requireInjection(RouterKey)
|
2022-11-18 18:44:20 +00:00
|
|
|
|
2022-11-18 18:56:21 +00:00
|
|
|
const getRouteParam = (name: string) => router.$currentRoute.value?.params?.[name]
|
|
|
|
const getCurrentScreen = () => router.$currentRoute.value?.screen
|
2024-10-13 17:37:01 +00:00
|
|
|
const isCurrentScreen = (...screens: ScreenName[]) => screens.includes(router.$currentRoute.value?.screen)
|
2022-11-18 18:56:21 +00:00
|
|
|
|
|
|
|
const onScreenActivated = (screen: ScreenName, cb: Closure) => {
|
2023-08-20 22:35:58 +00:00
|
|
|
isCurrentScreen(screen) && cb()
|
2022-11-18 18:56:21 +00:00
|
|
|
router.onRouteChanged(route => route.screen === screen && cb())
|
|
|
|
}
|
|
|
|
|
2022-11-18 18:44:20 +00:00
|
|
|
return {
|
2022-11-18 18:56:21 +00:00
|
|
|
getRouteParam,
|
|
|
|
getCurrentScreen,
|
|
|
|
isCurrentScreen,
|
|
|
|
onScreenActivated,
|
2024-01-18 11:13:05 +00:00
|
|
|
go: Router.go,
|
2022-11-18 18:44:20 +00:00
|
|
|
onRouteChanged: router.onRouteChanged.bind(router),
|
|
|
|
resolveRoute: router.resolve.bind(router),
|
2024-10-13 17:37:01 +00:00
|
|
|
triggerNotFound: router.triggerNotFound.bind(router),
|
2022-11-18 18:44:20 +00:00
|
|
|
}
|
|
|
|
}
|