URL的查询字符串不匹配react-router路由

URL with query string not matching react-router route

本文关键字:react-router 路由 不匹配 字符串 查询 URL      更新时间:2023-09-26

给定这个React路由器配置:

<Router history={browserHistory}>
    <Route path="/farm/:type?period=:period" component={Farm} />
    <Route path="/pen/:penId" component={Pen} />
    <Route path="/pen/:penId/cow/:cowId" component={Cow} />
    <Route path="*" component={Farm} />
</Router>

为什么http://localhost:8080/farm/average?period=day只匹配全能路由而不匹配第一条路由?

查询字符串参数不需要包含在<Route>定义中。

修改路由定义为<Route path="/farm/:type" component={Farm} />问题解决了

查询字符串参数在props.location.query

下仍然可用