Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enable (console) logging of router events in Vue

Somewhere in my Vue and Vuex application I suspect a (Vue) router push is triggered. To debug this I would like to enable logging of some sort, see all routing events pass by in my console. How can I do this?

I've searched the Vue Router guide for things like "logging", "events", and "console" but found nothing. In Angular I could just subscribe to all router events in a top level component, and I guess I'm looking for something similar.

So, in short: how can you enable logging for Vue Router events?

like image 953
Jeroen Avatar asked Mar 17 '19 11:03

Jeroen


People also ask

Can you console log in Vue?

Can you use console log in Vue? Console log can be used in Vue js application, however, it is disabled. The reason for this is that it is not a good practice to use methods on the console. But, if you want to test something, for example, if a method gets the data, by default, you can't use a console log to check this.

What is history mode in Vue router?

The default mode for vue-router is hash mode - it uses the URL hash to simulate a full URL so that the page won't be reloaded when the URL changes. To get rid of the hash, we can use the router's history mode, which leverages the history.


1 Answers

You need to log the changes in the router-view.

So, in the component that has your <router-view><router-view/> tag, simply watch for router change:

watch: {
    '$route' (to, from) {
      console.log('Route changed from ' + from.path + ' to ' + to.path); 
    }
}

Let me know if this isn't you're looking for.

like image 95
Ahmed Hammad Avatar answered Oct 16 '22 04:10

Ahmed Hammad