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?
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With