Search code examples
javascriptvue.jsbulmabuefy

Vue/Buefy - Hide dropdown on router-link click / SPA


I am having some issues using Buefy and Vue. I have a simple SPA (Single Page Application), that uses vue-router. I have a dropdown that looks like this:

  <b-dropdown class="user-dropdown" v-model="navigation" position="is-bottom-left">
    <a class="navbar-item" slot="trigger">
      <span class="tag is-rounded has-text-weight-semibold avatar">OB</span>
    </a>
    <div class="columns is-marginless">
      <div class="column has-text-centered">
        <router-link to="home">
          <b-icon class="is-large is-size-4" icon="ion-ionic ion-ios-search"></b-icon>
        </router-link>
      </div>
      <div class="column has-text-centered">
        <router-link to="dashboard">
          <b-icon class="is-large is-size-4" icon="ion-ionic ion-ios-contact"></b-icon>
        </router-link>
      </div>

    </div>
  </b-dropdown>

Getting the dropdown to show works fine. It also hides if I click anywhere on the page, outside of the visible dropdown.

My problem is, that I have a single page application, so when I "change" page (click a link), the dropdown is still visible when the page changes.

Example:

On below page:

www.example.com/#/home

I click on the dropdown, which opens, and then click on the dashboard link, which gives me:

www.example.com/#/dashboard

But the dropdown is still visible.

Anyone know how I can do, so the dropdown will toggle when I click on the links inside it?

Update: I use the Vue Dev tools in Chrome, and I can see that when I click on the dropdown so it toggles to active, I see this:

name:"active-change"
type:"$emit"
source:"<BDropdown>"
payload:Array[1]
0:true

And when I click anywhere on the page, to close the dropdown, it looks like this:

name:"active-change"
type:"$emit"
source:"<BDropdown>"
payload:Array[1]
0:false

Solution

  • Whatever the drop-down is watching for state isn't changing, because in a SPA it's just javascript. I don't know about the specifics regarding Buefy, but you'll have to set the state variable on router load, or watch some kind of variable to tell the menu that something in another part of your app is different.