重定向中不包含路由参数

Route parameter not included in redirect

前提

使用 react-router (v4) 它似乎无法正确重定向参数。

<Redirect from="/people/person/:id" to="/people/:id"/>

如果我访问 /people/person/123,它会直接重定向到 /people/:id


问题

这是最新版本的 React Router 缺少的功能还是我的语法不正确?还有其他方法可以实现吗?


其他说明

您的原始语法将不起作用。

您必须从 match.params.id 中获取 id 参见文档示例 https://reacttraining.com/react-router/web/api/Route:

<Route path="/user/:username" component={User}/>

const User = ({ match }) => {
  return <h1>Hello {match.params.username}!</h1>
}

你的看起来像:

<Route path='/people/person/:id' render={props => (
  <Redirect to={`/people/${props.match.params.id}`} />
)}/>