fix(vue): stub vue-router

This commit is contained in:
Benjamin Canac
2025-03-07 15:00:10 +01:00
parent af2f8987a3
commit 33ed3935a3

View File

@@ -6,7 +6,52 @@ import type { NuxtApp } from '#app'
import { useColorMode as useColorModeVueUse } from '@vueuse/core'
export { useHead } from '@unhead/vue'
export { useRoute, useRouter } from 'vue-router'
// Create stub implementations for vue-router
export const useRouteStub = () => ({
path: '',
name: null,
params: {},
query: {},
hash: '',
fullPath: '',
matched: [],
meta: {},
redirectedFrom: undefined
})
export const useRouterStub = () => ({
push: () => Promise.resolve(),
replace: () => Promise.resolve(),
go: () => Promise.resolve(),
back: () => Promise.resolve(),
forward: () => Promise.resolve(),
beforeEach: () => () => {},
afterEach: () => () => {},
getRoutes: () => [],
hasRoute: () => false,
currentRoute: ref({})
})
// Use a runtime check to determine which implementation to use
let _useRoute = useRouteStub
let _useRouter = useRouterStub
// Try to import from vue-router
try {
// This will throw if vue-router is not available
const vueRouter = await import('vue-router')
if (vueRouter.useRoute && vueRouter.useRouter) {
// Type assertion with unknown to fix type compatibility issues
_useRoute = vueRouter.useRoute as unknown as typeof _useRoute
_useRouter = vueRouter.useRouter as unknown as typeof _useRouter
}
} catch {
// Use the stubs if vue-router is not available
}
export const useRoute = _useRoute
export const useRouter = _useRouter
export { defineShortcuts } from '../composables/defineShortcuts'
export { useLocale } from '../composables/useLocale'