1

I'm using the following dropdown menu in tailwind & vue-router https://codepen.io/huphtur/pen/ordMeN.

I need to use <router-link to="/"> inside the dropdown menu, but I found out that doing that, once you click on a menu link the dropdown doesn't close as it should do. Using normal <a> elements the dropdown closes like it should.

This is the code:

dropdown element comp

<div v-if="isOpen == false" class="dropdown inline-block relative sm:ml-20">
          <button class="text-gray-700 font-normal py-2 px-4 rounded inline-flex items-center">
            <span class="mr-1 cursor-default">Vita</span>
          </button>
          <ul class="w-32 dropdown-menu absolute hidden text-gray-700 pt-1 shadow-lg z-10">
            <li > <router-link to="/biografia"> <a class="rounded-t bg-white hover:bg-gray-200 py-2 px-4 block whitespace-no-wrap text-sm"> Bio </a> </router-link> </li>
            <li > <router-link to="/tour"> <a class="bg-white hover:bg-gray-200 py-2 px-4 block whitespace-no-wrap text-sm"> Tour </a> </router-link> </li>
            <li > <router-link to="/foto"> <a class="bg-white hover:bg-gray-200 py-2 px-4 block whitespace-no-wrap text-sm"> Foto </a> </router-link> </li>
            <li > <router-link to="/video"> <a class="rounded-b bg-white  hover:bg-gray-200 py-2 px-4 block whitespace-no-wrap text-sm"> Video </a> </router-link> </li>
          </ul>
        </div>
}

css style in the comp

.dropdown:hover .dropdown-menu {
  display: block;
} 

How can I implement this functionality using <router-link to="/">?

Thanks!

nicoblue
  • 71
  • 2
  • 13

1 Answers1

0

router-link exposes a slot for you to enable customizations. I have used this in the past to apply styling to an outer li element when the router-link was active, but you can use the same approach for closing your dropdown as well.

  <router-link
    to="/"
    v-slot="{ href, route, navigate, isActive, isExactActive }"
  >
    <a
      :href="href"
      @click="isOpen = false; navigate($event)"
    >
      Homepage
    </a>
  </router-link>

Source

Mertcan Ekiz
  • 691
  • 1
  • 9
  • 22