Skip to content
This repository was archived by the owner on Apr 6, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
fix: add universal routerlink implementation of custom link
  • Loading branch information
danielroe committed Jun 9, 2022
commit 9ded9583ee0b3aae54c0d9621b9eabe5b62d4e81
7 changes: 7 additions & 0 deletions examples/routing/universal-router/app.vue
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ const timer = useState('timer', () => 0)
<NuxtLink to="/redirect" class="n-link-base">
Redirect
</NuxtLink>
<NuxtLink custom to="/redirect">
<template #default="{ href, navigate }">
<button @click="navigate">
Custom: {{ href }}
</button>
</template>
</NuxtLink>
</nav>
</template>

Expand Down
20 changes: 18 additions & 2 deletions packages/nuxt/src/app/plugins/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,24 @@ export default defineNuxtPlugin<{ route: Route, router: Router }>((nuxtApp) => {

nuxtApp.vueApp.component('RouterLink', {
functional: true,
props: { to: String },
setup: (props, { slots }) => () => h('a', { href: props.to, onClick: (e) => { e.preventDefault(); router.push(props.to) } }, slots)
props: {
to: String,
custom: Boolean,
replace: Boolean,
// Not implemented
activeClass: String,
exactActiveClass: String,
ariaCurrentValue: String
},
setup: (props, { slots }) => {
const navigate = () => handleNavigation(props.to, props.replace)
return () => {
const route = router.resolve(props.to)
return props.custom
? slots.default?.({ href: props.to, navigate, route })
: h('a', { href: props.to, onClick: (e) => { e.preventDefault(); return navigate() } }, slots)
}
}
})

if (process.client) {
Expand Down