Using Vue 3 and Vue Router, how can I create dynamic routes with multiple dynamic params?
Those params need to be able to be passed in any order.
In our webapp we have more than 500+ views, so we resolve the routes at runtime, loading the component (.vue
) when necessary, because it would be crazy to set each one of those 500 views in routes file.
Let's assume that we have a view called "Products", our clients have multiple filter options, and those filters are passed in the URL when they are filtering something. They can pass 0 parameters or 10+ parameters. It depends on what they are filtering.
Imagine that users can pass URLs like these below:
www.webapp.com/products
www.webapp.com/products/color/red
www.webapp.com/products/brand/myBrand/color/green
www.webapp.com/products/size/36/brand/myBrand
and multiple other dynamic possibilites.
I tried to add the routes using the wildcard *
, but it simply didn't work.
What I tried:
{path: '/products/:(.*)', ...}
{path: '/products/:params(.*)', ...}
{path: '/products/:(.*)*', ...}
{path: '/products/:params(.*)*', ...}
{path: '/products/*', ...}
{path: '/products-*', ...}
None of those attempts worked. Some threw an error, and some simply removed the parameters from the URL, leaving only /products
. In all attempts, this.$route.params
was empty.
How can I create dynamic routes with multiple dynamic parameters in any order with Vue 3 and Vue Router?
Obs: For the sake of 'Friendly URL', we are not able to use queries because they are ugly
(eg.: products?color=red&size=10&brand=my%20brand
)