Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions src/runtime/components/Link.vue
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,9 @@ import { computed } from 'vue'
import { isEqual } from 'ohash/utils'
import { useForwardProps } from 'reka-ui'
import { defu } from 'defu'
import { hasProtocol } from 'ufo'
import { reactiveOmit } from '@vueuse/core'
import { useRoute, useAppConfig } from '#imports'
import { useRoute, useAppConfig, useNuxtApp } from '#imports'
import { mergeClasses } from '../utils'
import { tv } from '../utils/tv'
import { isPartiallyEqual } from '../utils/link'
Expand All @@ -115,6 +116,7 @@ defineSlots<LinkSlots>()

const route = useRoute()
const appConfig = useAppConfig() as Link['AppConfig']
const nuxtApp = useNuxtApp()

const nuxtLinkProps = useForwardProps(reactiveOmit(props, 'as', 'type', 'disabled', 'active', 'exact', 'exactQuery', 'exactHash', 'activeClass', 'inactiveClass', 'to', 'href', 'raw', 'custom', 'class'))

Expand All @@ -130,7 +132,23 @@ const ui = computed(() => tv({
}, appConfig.ui?.link || {})
}))

const to = computed(() => props.to ?? props.href)
const to = computed(() => {
const path = props.to ?? props.href
if (!path) return path

// Skip external links and absolute URLs
if (props.external || (typeof path === 'string' && hasProtocol(path, { acceptRelative: true }))) {
return path
}

// Use localePath from `@nuxtjs/i18n` if available
const localePath = nuxtApp.$localePath as ((route: RouteLocationRaw) => RouteLocationRaw) | undefined
if (localePath) {
return localePath(path)
}
Comment on lines +145 to +148
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const localePath = nuxtApp.$localePath as ((route: RouteLocationRaw) => RouteLocationRaw) | undefined
if (localePath) {
return localePath(path)
}
const localeRoute = nuxtApp.$localeRoute as ((route: RouteLocationRaw) => RouteLocationRaw) | undefined
if (localeRoute) {
return localeRoute(path)
}

localePath will resolve to a path string, using localeRoute should preserve route properties (things like state)


return path
})

function isLinkActive({ route: linkRoute, isActive, isExactActive }: any) {
if (props.active !== undefined) {
Expand Down
Loading