我正在尝试使用查询字符串设置路由路径。内容如下:
www.mywebsite.com/results?query1=:query1&query2=:query2&query3=:query3
我会像这样过渡到“结果”组件:
<Route path="/" component={Main}>
<IndexRoute component={Home} />
<Route path="results?query1=:query1&query2=:query2&query3=:query3"
component={SearchResults} />
</Route>
在 SearchResults 容器中,我希望能够访问 query1、query2 和 query3 参数。
我没能让它发挥作用。我收到以下错误:
bundle.js:22627 警告:[react-router] 位置
“/results?query1=1&query2=2&query3=3”未匹配任何路由
我尝试按照以下指南中的步骤操作:(部分:查询字符串参数怎么样?)https://www.themarketingtechnographer.co/react-router-an-introduction/ https://www.themarketingtechnologist.co/react-router-an-introduction/
我可以在这里得到一些帮助吗?
如果您使用的是 React Router v2.xx,您可以通过以下方式访问查询参数:location.query
对象传递到您的 Route 组件。
换句话说,您可以重新设计您的路线,如下所示:
<Route path="results" component={SearchResults} />
然后在你的里面SearchResults
组件、使用this.props.location.query.query1
(类似于query2
and query3
) 访问查询参数值。
EDIT:React Router v3.xx 仍然是这种情况。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)