Is it possible to bind query values to props declaratively?
I want /my-foo?bar=my-bar
to pass the props {foo: "my-foo", bar: "my-bar"}
.
I'm currently using something like this:
export default new Router({
routes: [
{
path: "/:foo",
name: "Foo",
component: FooPage,
props: route => ({ foo: route.params.foo, bar: route.query.bar})
}
]
});
And I'm looking for something like:
export default new Router({
routes: [
{
path: "/:foo?bar=:bar",
name: "Foo",
component: FooPage,
props: true
}
]
});
I'm using vue-router 2.3.1
I don't understand the problem with your current approach; solves your use case just fine.
That said, you could try
Object.assign
, something like this:... you can also try a more modern approach by using object spread (if you have babel correctly configured) ...
I am not aware of any declarative directive for that, but I do like Ricardos general approach. What might be a problem with that is that it unconditionally binds all query parameters as a prop, so one can modify any predefined props of that component just by adding it to the url.
If you want a filter and still reusability, you can define a helper function and bind the query in a less verbose way:
With an implementation like the following. Note that this is TypeScript and has type annotations. Just remove them if you need plain JavaScript.