如何在 vue-router 中添加可选路由?

How to add optional route in vue-router?

我有一个这样的按钮:

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

我的上述路线在 route.js 文件中定义为:

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

现在,我的问题是如何让我的路线接受有条件的 url 道具,例如 /foo/bar/1/foo/bar 因为现在,它只接受 /foo/bar

path: 'foo/bar/:id'

在这里你可以阅读它:https://router.vuejs.org/en/essentials/dynamic-matching.html

如果要将其作为可选参数,请使用问号?

'foo/bar/:id?'