2023-03-14 React.js 通过this.props.match.params拿到传递的id为空或者undefined

前言:react+antd实现点击按钮跳转页面,跳转代码如下:

this.props.history.push('/my/' + id)

已知my在router中已注册:

<Route path="/my/" component={My} />

解决方案:需要在router修改以下,即:

<Route path="/my//:id" component={My} />

原因:要使URL路径的一部分成为参数,需按以下格式写:

<Route path='/authors/:authorName' component={FilterBooks}/>

参考资料:https://cloud.tencent.com/developer/ask/sof/220850

其实不用上面的方案也可以通过this.props.location来获取传参。

posted @ 2023-03-14 13:48  叶乘风  阅读(129)  评论(0编辑  收藏  举报