mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-16 10:54:49 +00:00
24 lines
704 B
TypeScript
24 lines
704 B
TypeScript
|
import { computed, reactive } from 'vue'
|
||
|
import type { Router, RouteLocationNormalizedLoaded } from 'vue-router'
|
||
|
import { useNuxtApp } from '#app'
|
||
|
|
||
|
export const useRouter = () => {
|
||
|
return useNuxtApp().$router as Router
|
||
|
}
|
||
|
|
||
|
export const useRoute = (): RouteLocationNormalizedLoaded => {
|
||
|
const nuxtApp = useNuxtApp()
|
||
|
if (nuxtApp._route) {
|
||
|
return nuxtApp._route
|
||
|
}
|
||
|
|
||
|
const currentRoute = (nuxtApp.$router as Router).currentRoute
|
||
|
|
||
|
// https://github.com/vuejs/vue-router-next/blob/master/src/router.ts#L1192-L1200
|
||
|
nuxtApp._route = reactive(Object.fromEntries(
|
||
|
Object.keys(currentRoute.value).map(key => [key, computed(() => currentRoute.value[key])])
|
||
|
) as any)
|
||
|
|
||
|
return nuxtApp._route
|
||
|
}
|