react-router 传参 id的新手问题,请指点

//▼我有一个链接跳到另一个页面,并附带一个id=xxxxxxxx
<Link to="/news/newsArticle" params={{id: 12}} />
//▼这是它将来跳到的目标组件
<Route path="/news" getComponent={news}>
    <IndexRoute path="/news"  getComponent={newsList}/>
    //▼对就在这▼
    <Route path="/news/newsArticle" getComponent={newsArticle}/>
    //我试过这样地址加/:id 直接不能成功跳转
    //<Route path="/news/newsArticle/:id" getComponent={newsArticle}/>
</Route>

当前已经能成功跳转到目标页,但是:
▼问题是:我在目标组件打印他们,发现里面没有id,我如何能得到id

constructor(props){
    super(props);
}
render() {
    console.log(this.props.params)
    console.log(this.props.params.id)
.....

感谢您的到来,感谢您提供的帮助。好人一生平安。

-谢谢

阅读 4.1k
2 个回答
<Link to="/news/newsArticle/12" />

<Route path="/news" getComponent={news}>
    <IndexRoute path="/news"  getComponent={newsList}/>
    <Route path="/news/newsArticle/:id" getComponent={newsArticle}/>
</Route>

有两个参数,params 和location 这里面你打印一下都会有你要的信息(this.props.pramas || this.props.location)

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题