我一直试图捕获一个未定义的URL,并将其传递给将处理该URL(显示消息)的组件。我不确定如何传递已捕获的当前URL。谢谢。
<Switch>
<Route exact path='*' render={() => <HttpError data={"Undefined URL: " + this.props.match}/>}/>
</Switch>
将路线道具传递到要渲染的组件。访问props.match.url
以获取要渲染的路线的URL。
<Switch>
<Route
exact
path='*'
render={(routeProps) => (
<HttpError data={"Undefined URL: " + routeProps.match.url} />
)}
/>
</Switch>
如果您将其用作一种“ 404”路线,则可以将其放置在您的最后Switch
,删除exact
道具(也可以删除path
道具),如果在匹配之前没有任何内容,则它将由返回/渲染Switch
。
<Switch>
// ... all other routes
<Route
render={(routeProps) => (
<HttpError data={"Undefined URL: " + routeProps.match.url} />
)}
/>
</Switch>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句