如何从右到左匹配React Router路径

时间:2018-12-11 16:36:32

标签: javascript react-router

使用React路由器(更具体地说,它依赖于path-to-regex)。我具有下面的URL结构,我需要从URL中提取ID和子段,并且工作正常!

但是,我们的数据Feed中出现了一些URL,这些URL引起了一些问题:

<Route
    exact
    path="/:slug-v-:id/"
    component={({ match }) => {
        // http://example.com/this-is-a-url-v-42-12345/
        console.log(match.params.slug); // 'this-is-a-url'
        console.log(match.params.id); // '42-12345'
        console.log('It works!'); // 'It works!'

        // http://example.com/this-is-a-v-annoying-url-v-42-12345/
        console.log(match.params.slug); // 'this-is-a'
        console.log(match.params.id); // 'annoying-url-v-42-12345'
        console.log('It breaks :('); // 'It breaks :('
    }}
/>

子弹中出现的多余的 -v-破坏了参数。我想反向匹配路径,即从右到左,或者如果可能的话,使用另一种方法。

1 个答案:

答案 0 :(得分:0)

您可能会使用 路径=“ /:slug /:id /”