在路由器中,我们可以使用{this.props.match.params.id}来匹配我们想要映射的某个对象的详细信息。
<Route path="/userDetail/:id" exact component={UserDetail} />
在该组件中,我们可以访问:id
但在自定义路线中我们如何达到这个目标?我在this.props.computedMatch.params.id中找到了:id
但我想把它传递给this.props.match
let PrivateRoute = ({ component: ChildComponent, isLogin, ...rest}) => {
return <Route render={props => {
if (!this.props.get.isLogin) {
return <Redirect to="/login" />;
} else {
return <ChildComponent {...props} />
}
}} />
<Switch>
<PrivateRoute path="/userDetail/:id" exact component={UserDetail} />
</Switch>
您还需要包含传递到路线中的其他道具:
<ChildComponent {...props} {...rest} />