我有一个组件想要显示在除根路径之外的所有路径上。因此,我没有提供组件的所有路径Route,而是编写了以下内容:
<Route exact path={/^\/.+$/} component={() =>
<div><img src="../../../assets/AndreyBogdanov2.jpg" className="me" /></div>
} />
Run Code Online (Sandbox Code Playgroud)
该正则表达式匹配开头斜杠后包含任何字符的所有字符串。它按预期工作,但是在控制台中我看到一个错误:
Warning: Failed prop type: Invalid prop `path` of type `regexp` supplied to `Route`, expected `string`.
in Route (created by App)
in App
in Router (created by BrowserRouter)
in BrowserRouter
Run Code Online (Sandbox Code Playgroud)
我并不是在抱怨,而是想知道,它是如何运作的?
在 prop 的文档中,path它说它需要是任何可以path-to-regexp理解的 URL,而在 的path-to-regexp文档中,它说 regex 是一个有效的参数。PropTypes所以基本上,当遇到字符串以外的内容时,组件会抛出错误。
基本上我想抓住<Route path="/:slug" />但除了/not-found. 我尝试使用看起来像/:slug(^(?!.*not-found).*$)但它不起作用的路径到正则表达式的自定义匹配参数。它在这里不起作用http://forbeslindesay.github.io/express-route-tester/,但它在https://regex101.com/中起作用
我想知道当我试图捕捉时如何排除路径/:slug?