2

I'm creating a search engine with multiple filters users can pick, along with a query. All of those are optional and add up to the filtered result set.

So all these cases are possible and being properly stored in an object of keywords:

  • Searching with a query
  • Searching by country (without specifying any query)
  • Searching by type (without specifying any query)
  • Searching a query with a country and a type
  • ...

What would be the best way of handling this with Vue Router? I want the search route to be updated with whatever filters are picked.

As far as I can tell I can't use multiple optional parameters and unless I specify all possible combinations (which is a bit silly) like:

export const routes = [
{ path: '/search', name: 'search', component: Search, children: [
    { path: 'query/:query, component: Search },
    { path: 'type/:type', component: Search },
    { path: 'country/:country', component: Search },
    ...
]},

... I don't see any other way of solving the problem. What would be the best option?

1 Answer 1

2

One option can be to use query params, so your URL will look like this:

/search?country=sire&type=middleearth

You can access these from $route.query.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.