[Vuejs]-Vue-router : why watch on $route trigger when previous queries rewrite with same values

1👍

On top of the answer that Cristian provided, you could also even double-check if your stuff has changed before even pushing a new object to your router.

Like this

checkIfUpdateNeeded && this.$router.push({queries: {name: 'kevin', age:21 } })

That way, you will have less moving parts and you won’t have a trigger in the watcher for "nothing", especially if you’re pushing a bigger object and want to make a deep-diff between 2 objects.

👤kissu

3👍

The object you are pushing on the router is always different, that’s why the $route watch is launched.
You can compare the data you receive in the watch, so that when they are different then you invoke the API:

watch:{
   '$route' (newRoute, lastRoute){
       // Check if query is different
       // call API
   }
}

Leave a comment