question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

nuxt-link does not resolve inside an as prop

See original GitHub issue

Environment

  • Operating System: Linux
  • Node Version: v16.14.2
  • Nuxt Version: 3.0.0-27501460.04a72f8
  • Package Manager: npm@8.5.5
  • Builder: vite
  • User Config: css, buildModules, vite, build
  • Runtime Modules: -
  • Build Modules: @pinia/nuxt@0.1.8

Reproduction

Code:

<DisclosureButton as="nuxt-link" to="/dashboard" class="block px-4 py-2 text-base font-medium text-gray-500 hover:text-gray-800 hover:bg-gray-100">
  Go to dashboard
</DisclosureButton>

HTML:

<nuxt-link disabled="false" to="/dashboard" class="block px-4 py-2 text-base font-medium text-gray-500 hover:text-gray-800 hover:bg-gray-100">
  Go to dashboard
</nuxt-link>

Describe the bug

This is not exactly a bug… But if it can be done somehow. The expected result would be the same as if I passed the tag ‘button’.

Example: Code:

<DisclosureButton as="button" to="/dashboard" class="block px-4 py-2 text-base font-medium text-gray-500 hover:text-gray-800 hover:bg-gray-100">
  Go to dashboard
</DisclosureButton>

HTML:

<button type="button" to="/dashboard" class="block px-4 py-2 text-base font-medium text-gray-500 hover:text-gray-800 hover:bg-gray-100">
  Go to dashboard
</button>

Additional context

No response

Logs

No response

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:14 (5 by maintainers)

github_iconTop GitHub Comments

3reactions
oyedcommented, Apr 19, 2022

I’m guessing it just can’t resolve the button, since components are more “just in time” imports now. Try this:

<template>
    <DisclosureButton :as="NuxtLink" to="/dashboard" class="block px-4 py-2 text-base font-medium text-gray-500 hover:text-gray-800 hover:bg-gray-100">
        Go to dashboard
    </DisclosureButton>
</template>

<script lang="ts" setup>
    const NuxtLink = resolveComponent('nuxt-link');
</script>
2reactions
skaaptjopcommented, Oct 3, 2022

I do realise this discussion might not be specifically Nuxt related and is more related to Headless UI with Nuxt. @John-Church that is what I found too. I could use :as=NuxtLink successfully to render but it wouldn’t close the DisclosurePanel when activated.

So I swapped out each DisclosureButton with a NuxtLink and added a @click handler to the container that holds those links. The @click handler simply calls close() manually and then every works fine. This is mentioned in HeadlessUI docs for manually closing the panel.

It looks something like this:

<DisclosurePanel class="sm:hidden" v-slot="{ close }">
  <div @click="close()">
    <NuxtLink to="/">Home</NuxtLInk>
  </div>
</DisclosurePanel>

This works great. Also has the added benefit of being able to track and style the active link in the menu as well. I used a custom component for this something like:

export default defineNuxtLink({
    componentName: 'NavbarMobileNuxtLink',
    exactActiveClass: 'text-indigo-700 border-indigo-500 bg-indigo-50'
})
Read more comments on GitHub >

github_iconTop Results From Across the Web

vue.js - How to interpolate nuxt-link's `to` prop? - Stack Overflow
vue . The problem is that, in my index page, I cannot get <NuxtLink> to go to single post pages. I have a...
Read more >
Built-in Components - Nuxt
This prop will be passed to <RouterView> so that your transitions will work correctly inside dynamic pages. There are 2 ways to handle...
Read more >
Button | Components - BootstrapVue
You can specify the button's type by setting the prop type to 'button' , 'submit' or 'reset' . The default type is 'button'...
Read more >
Language Aware Nuxt.js Routing - Abdelrahman Awad
These types of applications usually do not include the language code in ... then render a nuxt-link component with the newly formed to...
Read more >
Passing Props to Route Components | Vue Router
While this is not necessarily a bad thing, we can decouple this behavior with a props option: We can replace. const User =...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found