vue-router path-to-regexp 不匹配可选路由路径参数组

vue-router path-to-regexp does not match optional route path parameter group

简介

我正在与 vue.js 的官方 vue-router 合作。

我目前正在尝试使用 dynamic route matching. Since vue-router uses path-to-regexp under the hood, one can use regex in a route path as described here. You can find an example of the vue-router using this feature here 来匹配路线。

问题

这是我的路线:

{
    path: '/a/:id/(r/:generic1/)?s/:name/a'
}

这里有一些确实有效的例子:

'/a/1234/s/Foo/a' // => {0: undefined, id: "1234", name: "Foo"}
'/a/23456/s/Bar/a' // => {0: undefined, id: "23456", name: "Bar"}

这里有些例子不起作用,但应该:

'/a/1234/r/Bar/s/Foo/a' // => {id: "1234", generic1: "Bar", name: "Foo"}
'/a/23456/r/Baz/s/Goo/a' // => {id: "1234", generic1: "Baz", name: "Goo"}

..我在这里做错了什么?其中一个示例说明如下:

make part of the path optional by wrapping with parens and add "?" [sic]

我认为这些路径应该与给定的路线相匹配。

根据this comment,您不能将参数放在正则表达式区域中。

我认为你可以做这样的事情/a/:id/r?/:generic1?/s/:name/a这应该匹配你所有的例子并且还保留了generic1参数。

我用 Express Route Tester 验证了这个解决方案。