Cant assign `href` to `UButton` or `ULink`
Couldnt assign
href
attribute to UButton
component, then i checked it out which is uses ULink
underline. I tried on it as well but it doesnt render it as well (want to disable spa navigation to spesific pages)
Can't really understand whats wrong with the component: https://github.com/nuxt/ui/blob/dev/src/runtime/components/elements/Link.vue Does anyone have a clue? https://github.com/nuxt/ui/blob/dev/src/runtime/utils/link.ts everything looks ok with props though...
3 Replies
Hi @Lynx. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
----------------------
Based on the knowledge sources provided, I can help explain the issue you're encountering with
UButton
and ULink
components in Nuxt UI.
The UButton
and ULink
components are designed to work with Vue Router and Nuxt's routing system. They don't directly support the href
attribute in the way you're trying to use it.
For UButton
, you should use the to
prop instead of href
. The to
prop is used for internal routing. Here's how you can modify your code:
For ULink
, you're close, but there are a few adjustments needed:
1. The href
prop should be a string, not an object.
2. If you want to disable SPA navigation for specific pages, you can use the external
prop.
Here's the corrected version:
The external
prop forces the link to be rendered as an <a>
tag instead of a Vue Router RouterLink
, which should disable SPA navigation for this specific link Nuxt Docs - NuxtLink Props.
If you specifically need to use the href
attribute and want full control over how the link is rendered and how navigation works, you can use the custom
prop along with scoped slots. Here's an example:
This approach gives you more flexibility in handling the link behavior Stack Overflow - Vue Router with custom attribute.
Remember that ULink
is a wrapper around NuxtLink
, which in turn is based on Vue Router's RouterLink
. It's designed to work seamlessly with Nuxt's routing system, so using the to
prop is generally the recommended approach for internal links.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------<@256524650622746624> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
external
really does the job. but docs reflects like href should work