I have an app where a component, Foo
, is used as a template for a route, app/foo
. This component has child components which are also used as templates for routes: app/foo/bar
, app/foo/baz
, etc.
I am binding a global vuex
store object to the query
property of the route so that if the object is set to, say, { a: 1, b: 2 }
, the url params will change to ?a=1&b=2
; and if the user changes the params to, say, ?a=0&b=0
, the object will update to be { a: 0, b: 0 }
. This is all working as expected.
The trouble I'm having is that, when I then route to any path under app/foo
, I lose the route's query
property and the url params disappear.
I can pass the query
object dynamically like so:
this.$router.push({ name: 'bar', query: this.$route.query });
But, this is quickly getting hard to maintain.
I know Vue Router provides added life-cycle events, and I thought I should be able to update the query on the Foo
component's beforeRouteUpdate
event. I've made a few different attempts, but no dice:
beforeRouteUpdate (to, from, next) {
// This gives me an error saying that the query is read-only
to.query = from.query;
// This does not update the query
this.$router.replace({ query: from.query })
next();
},
Edit:
I also tried setting a watcher on $route
to replace the query when the route changes. However, this causes an infinite loop:
watch: {
$route(to, from) {
this.$router.replace({ query: from.query });
}
}
Does anyone know a way to make the Vue Router query persistent at a component-level?
to.path != from.path
), there is some undesirable behavior. But I'll make an answer with my current (unideal) solution based on your comment. Thanks! – thanksd