重定向中不包含路由参数
Route parameter not included in redirect
前提
使用 react-router (v4) 它似乎无法正确重定向参数。
<Redirect from="/people/person/:id" to="/people/:id"/>
如果我访问 /people/person/123
,它会直接重定向到 /people/:id
。
问题
这是最新版本的 React Router 缺少的功能还是我的语法不正确?还有其他方法可以实现吗?
其他说明
正在 <Switch>
标签中使用。
我看过这里的语法:https://reacttraining.com/react-router/web/api/Redirect
我在这里看到了一些额外的示例语法:https://github.com/ReactTraining/react-router/issues/1034
您的原始语法将不起作用。
您必须从 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}`} />
)}/>
前提
使用 react-router (v4) 它似乎无法正确重定向参数。
<Redirect from="/people/person/:id" to="/people/:id"/>
如果我访问 /people/person/123
,它会直接重定向到 /people/:id
。
问题
这是最新版本的 React Router 缺少的功能还是我的语法不正确?还有其他方法可以实现吗?
其他说明
正在
<Switch>
标签中使用。我看过这里的语法:https://reacttraining.com/react-router/web/api/Redirect
我在这里看到了一些额外的示例语法:https://github.com/ReactTraining/react-router/issues/1034
您的原始语法将不起作用。
您必须从 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}`} />
)}/>