Vuejs: Event on route change

kipris picture kipris · Sep 25, 2017 · Viewed 118.6k times · Source

In my main page I have dropdowns that show v-show=show by clicking on link @click = "show=!show" and I want to set show=false when I change route. Advise me please how to realize this thing.

Answer

Vamsi Krishna picture Vamsi Krishna · Sep 25, 2017

Setup a watcher on the $route in your component like this:

watch:{
    $route (to, from){
        this.show = false;
    }
} 

This observes for route changes and when changed ,sets show to false