mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-22 05:35:13 +00:00
fix(nuxt): handle external links to named route objects (#27829)
This commit is contained in:
parent
e9a531eb1f
commit
07e1818f30
@ -167,8 +167,10 @@ export function defineNuxtLink (options: NuxtLinkOptions) {
|
||||
if (!to.value || isAbsoluteUrl.value) { return to.value as string }
|
||||
|
||||
if (isExternal.value) {
|
||||
const path = typeof to.value === 'object' ? resolveRouteObject(to.value) : to.value
|
||||
return resolveTrailingSlashBehavior(path, router.resolve /* will not be called */) as string
|
||||
const path = typeof to.value === 'object' && 'path' in to.value ? resolveRouteObject(to.value) : to.value
|
||||
// separately resolve route objects with a 'name' property and without 'path'
|
||||
const href = typeof path === 'object' ? router.resolve(path).href : path
|
||||
return resolveTrailingSlashBehavior(href, router.resolve /* will not be called */) as string
|
||||
}
|
||||
|
||||
if (typeof to.value === 'object') {
|
||||
|
@ -30,17 +30,16 @@ vi.mock('../src/app/composables/router', () => ({
|
||||
return withQuery(to.path || '', to.query || {}) + (to.hash || '')
|
||||
},
|
||||
useRouter: () => ({
|
||||
resolve: (route: string | RouteLocation & { to?: string }): Partial<RouteLocation> & { href?: string } => {
|
||||
resolve: (route: string | RouteLocation): Partial<RouteLocation> & { href: string } => {
|
||||
if (typeof route === 'string') {
|
||||
return { href: route, path: route }
|
||||
return { path: route, href: route }
|
||||
}
|
||||
return {
|
||||
path: route.path || `/${route.name?.toString()}`,
|
||||
query: route.query || undefined,
|
||||
hash: route.hash || undefined,
|
||||
href: route.path || `/${route.name?.toString()}`,
|
||||
}
|
||||
return route.to
|
||||
? { href: route.to }
|
||||
: {
|
||||
path: route.path || `/${route.name?.toString()}` || undefined,
|
||||
query: route.query || undefined,
|
||||
hash: route.hash || undefined,
|
||||
}
|
||||
},
|
||||
}),
|
||||
}))
|
||||
@ -133,6 +132,10 @@ describe('nuxt-link:propsOrAttributes', () => {
|
||||
expect(nuxtLink({ to: { path: '/to' }, external: true }).props.href).toBe('/to')
|
||||
})
|
||||
|
||||
it('resolves route location object with name', () => {
|
||||
expect(nuxtLink({ to: { name: 'to' }, external: true }).props.href).toBe('/to')
|
||||
})
|
||||
|
||||
it('applies trailing slash behaviour', () => {
|
||||
expect(nuxtLink({ to: { path: '/to' }, external: true }, { trailingSlash: 'append' }).props.href).toBe('/to/')
|
||||
expect(nuxtLink({ to: '/to', external: true }, { trailingSlash: 'append' }).props.href).toBe('/to/')
|
||||
@ -225,6 +228,7 @@ describe('nuxt-link:propsOrAttributes', () => {
|
||||
it('forwards `to` prop', () => {
|
||||
expect(nuxtLink({ to: '/to' }).props.to).toBe('/to')
|
||||
expect(nuxtLink({ to: { path: '/to' } }).props.to).toEqual({ path: '/to' })
|
||||
expect(nuxtLink({ to: { name: 'to' } }).props.to).toEqual({ name: 'to' })
|
||||
})
|
||||
})
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user