koel/resources/assets/js/composables/useRouter.ts

26 lines
860 B
TypeScript
Raw Normal View History

2022-11-18 18:44:20 +00:00
import { RouterKey } from '@/symbols'
import { requireInjection } from '@/utils'
export const useRouter = () => {
const router = requireInjection(RouterKey)
2022-11-18 18:56:21 +00:00
const getRouteParam = (name: string) => router.$currentRoute.value?.params?.[name]
const getCurrentScreen = () => router.$currentRoute.value?.screen
const isCurrentScreen = (...screens: ScreenName[]) => screens.includes(router.$currentRoute.value?.screen!)
const onScreenActivated = (screen: ScreenName, cb: Closure) => {
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,
2022-11-18 18:44:20 +00:00
go: router.go.bind(router),
onRouteChanged: router.onRouteChanged.bind(router),
resolveRoute: router.resolve.bind(router),
triggerNotFound: router.triggerNotFound.bind(router),
}
}