If you want Vue to refresh the same component when the route query changes, you can give a key
to the <router-view
it is mounted into and navigate to the page with the same route name or path.
For example:
Mounting point:
<router-view
:key="$route.fullPath"
/>
Component navigation, assuming the route name is blog
<router-link :to={ name: 'blog', query: { count: 10 } }>Link to the same route</router-link>
This is useful when you need to reset the component's data when navigating to the same component with different data.
If the component you want to reset is not the route component, you can reset its data using the watch
option while storing the original data.
For instance:
data () {
return {
initialData: {
// some initial data
},
data: {}
}
},
watch: {
'$route.fullPath': {
immediate: true, // Immediate option to call watch handler on first mount
handler () {
this.resetData()
}
}
},
methods: {
resetData () {
this.data = Object.assign({}, this.initialData)
},
},
Keep in mind that any $route
options can be watched, and you can add additional conditions to the handler using the next and previous arguments.