|
6 | 6 | :class="classes" |
7 | 7 | target="_blank" |
8 | 8 | rel="noopener noreferrer" |
9 | | - :href="to" |
| 9 | + :href="String(to)" |
10 | 10 | :tabindex="disabled ? -1 : undefined" |
11 | 11 | :aria-disabled="disabled" |
12 | 12 | > |
|
26 | 26 | </a> |
27 | 27 | </template> |
28 | 28 |
|
29 | | -<script> |
30 | | -import { RouterLink, START_LOCATION, useLink, useRoute } from 'vue-router' |
31 | | -import { computed, defineComponent, toRefs } from 'vue' |
| 29 | +<script setup lang="ts"> |
| 30 | +import { START_LOCATION, useLink, useRoute } from 'vue-router' |
| 31 | +import { computed, useAttrs, } from 'vue' |
| 32 | +import type { RouterLinkProps } from 'vue-router' |
32 | 33 |
|
33 | | -export default defineComponent({ |
34 | | - props: { |
35 | | - ...RouterLink.props, |
36 | | - disabled: Boolean, |
37 | | - }, |
| 34 | +const { replace, to, disabled } = defineProps<RouterLinkProps & {disabled?: boolean}>() |
| 35 | +const attrs = useAttrs() |
38 | 36 |
|
39 | | - setup(props, { attrs }) { |
40 | | - const { replace, to, disabled } = toRefs(props) |
41 | | - const isExternalLink = computed( |
42 | | - () => typeof to.value === 'string' && to.value.startsWith('http') |
43 | | - ) |
| 37 | +const isExternalLink = computed( |
| 38 | + () => typeof to === 'string' && to.startsWith('http') |
| 39 | +) |
44 | 40 |
|
45 | | - const currentRoute = useRoute() |
| 41 | +const currentRoute = useRoute() |
46 | 42 |
|
47 | | - const { route, href, isActive, isExactActive, navigate } = useLink({ |
48 | | - to: computed(() => (isExternalLink.value ? START_LOCATION : to.value)), |
49 | | - replace, |
50 | | - }) |
51 | | -
|
52 | | - const classes = computed(() => ({ |
53 | | - // allow link to be active for unrelated routes |
54 | | - 'router-link-active': |
55 | | - isActive.value || currentRoute.path.startsWith(route.value.path), |
56 | | - 'router-link-exact-active': |
57 | | - isExactActive.value || currentRoute.path === route.value.path, |
58 | | - })) |
59 | | -
|
60 | | - return { attrs, isExternalLink, href, navigate, classes, disabled } |
61 | | - }, |
| 43 | +const { route, href, isActive, isExactActive, navigate } = useLink({ |
| 44 | + to: computed(() => (isExternalLink.value ? START_LOCATION : to)), |
| 45 | + replace, |
62 | 46 | }) |
| 47 | +
|
| 48 | +const classes = computed(() => ({ |
| 49 | + // allow link to be active for unrelated routes |
| 50 | + 'router-link-active': |
| 51 | + isActive.value || currentRoute.path.startsWith(route.value.path), |
| 52 | + 'router-link-exact-active': |
| 53 | + isExactActive.value || currentRoute.path === route.value.path, |
| 54 | +})) |
63 | 55 | </script> |
0 commit comments