hidar
hidar

Reputation: 5939

How to add optional route in vue-router?

I have a button like this:

<router-link :to="{path: '/foo/bar', params: {id: 1}">

and my route for the above is defined in route.js file as:

...
{
 path: 'foo/bar',
 component: fooComponent()
}
...

Now, my question is how do I make my route accept a conditional url prop like /foo/bar/1 and /foo/bar because now, it only accepts /foo/bar

Upvotes: 0

Views: 1775

Answers (2)

latovic
latovic

Reputation: 927

If you want to make it as optional parameter, use question mark ?.

'foo/bar/:id?'

Upvotes: 3

dartist21
dartist21

Reputation: 319

path: 'foo/bar/:id'

Here you can read about it: https://router.vuejs.org/en/essentials/dynamic-matching.html

Upvotes: 1

Related Questions