在反应路由器中使用连字符匹配路由 [英] Matching routes with hyphens in react-router
本文介绍了在反应路由器中使用连字符匹配路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这样的网址;
http://0.0.0.0/country/bosnia-and-herzegovina-644
这是我的路线声明
<Route path="/country/:countrySlug-:countryId" component={CountryPage} />
所以这在连字符打破时不起作用,我该如何更改它,所以我只提取最后一个连字符作为参数,我真的不想更改我的 slug 生成系统以使用不同的字符,因为这些是最易读的恕我直言,但仅用于信息,而不是内容加载.
So this doesnt work as the hyphen breaks it, how can I change it so I only extract the last hyphen as the param, I don't really want to change my slug generation system to use a different character as these are the most readable IMHO, but are only used for information, not content loading.
谢谢
推荐答案
我不知道您是否可以这样做,但也许您可以这样做:
I don't know if you can do that, but maybe you could do this:
<Route path="/country/:country" component={CountryPage} />
然后在组件中:
const countryId = this.props.match.params.country.slice('-').pop();
这篇关于在反应路由器中使用连字符匹配路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文