用负前瞻反应路由器 [英] react router with negative lookahead
问题描述
基本上我试图捕捉 <Route path="/:slug"/>
但不是 /not-found
.我尝试使用看起来像 /:slug(^(?!.*not-found).*$)
的路径到正则表达式的自定义匹配参数,但它不起作用.它在这里不起作用 http://forbeslindesay.github.io/express-route-tester/,但它确实适用于 https://regex101.com/
Basically I am trying to catch <Route path="/:slug" />
but other than /not-found
.
I try to use path-to-regex's Custom Match Parameters that looks like /:slug(^(?!.*not-found).*$)
but it doesn't work.
It does not work here http://forbeslindesay.github.io/express-route-tester/, but it does work in https://regex101.com/
我想知道如何在尝试捕获 /:slug
时排除路径?
I am wondering how can I exclude a path when I am trying to catch /:slug
?
推荐答案
我找到了 这个问题在路径到正则表达式存储库中说这不受支持,但使用正则表达式是.然而,react-router 只支持路径作为字符串目前.
I found this issue in path-to-regex repo saying this is not supported, but using RegEx is. However react-router only support path as string currently.
此外,您可以在这个演示
所以要获得这种行为,我认为要走的路是通过 <Route path= 查看组件渲染的
并有条件地渲染一个未找到的组件.match
或 location
道具"/:slug"/>
So to get this behaviour I think the way to go is to look at the match
or location
props of the component render by <Route path="/:slug" />
and conditionally render a not-found component.
这篇关于用负前瞻反应路由器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!