在react-router中用连字符匹配路由

dhj*_*dhj 5 javascript reactjs react-router

我有这样的网址;

http://0.0.0.0/country/bosnia-and-herzegovina-644
Run Code Online (Sandbox Code Playgroud)

这是我的路线声明

<Route path="/country/:countrySlug-:countryId" component={CountryPage} />
Run Code Online (Sandbox Code Playgroud)

所以这不起作用,因为连字符打破了它,我怎样才能改变它,所以我只提取最后一个连字符作为参数,我真的不想改变我的slug生成系统以使用不同的字符,因为这些是最易读的恕我直言,但仅用于信息,而不是内容加载。

谢谢

Tom*_*zyk 1

我不知道你是否可以这样做,但也许你可以这样做:

<Route path="/country/:country" component={CountryPage} />
Run Code Online (Sandbox Code Playgroud)

然后在组件中:

const countryId = this.props.match.params.country.slice('-').pop();
Run Code Online (Sandbox Code Playgroud)