What is the best way to implement dynamic routes with multiple dynamic parameters in any order using Vue 3 and Vue Router?
These parameters should be able to be passed in any order.
In our web application, we have over 500 views which makes it impractical to set each one in the routes file. Therefore, we need to resolve the routes at runtime by loading the necessary component (.vue
) when required.
Consider a view named "Products" where clients can use multiple filter options, passing them in the URL while filtering. The number of parameters they pass varies from 0 to more than 10 depending on their filtering requirements.
Users should be able to create URLs like the examples 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 many other dynamic possibilities.
I attempted to add routes using wildcard *
, but had no success.
Here are some examples of what I tried:
{path: '/products/:(.*)', ...}
{path: '/products/:params(.*)', ...}
{path: '/products/:(.*)*', ...}
{path: '/products/:params(.*)*', ...}
{path: '/products/*', ...}
{path: '/products-*', ...}
None of these attempts worked as expected. Some threw errors, while others removed parameters from the URL, leaving only /products
. In all cases, this.$route.params
remained empty.
How can I successfully create dynamic routes with multiple dynamic parameters in any order using Vue 3 and Vue Router?
Please note that for the sake of 'Friendly URL,' we cannot use queries because they are considered ugly
(e.g.,
products?color=red&size=10&brand=my%20brand
)