用负前瞻反应路由器 [英] react router with negative lookahead

查看:35
本文介绍了用负前瞻反应路由器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上我试图捕捉 <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= 查看组件渲染的 matchlocation 道具"/: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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆