今天爱分享给大家带来React 路由传参的方式有哪些?【最全总结】,希望能够帮助到大家。
1. 直接在to属性的值后面加?跟上参数
通过this.props.location.search 可以得到?及之后的字符串,在自己进行分析
商品id:{item.goods_id}
接收: this.props.location.search //输出为 ?id=5 属性string类型
2. 使用query或params进行传值
商品id:{item.goods_id}
接收:this.props.location.query.id
商品id:{item.goods_id}
接收:this.props.location.params.id
商品id:{item.goods_id}
接收: this.props.location.state.id
同query差不多,只是属性不一样,而且state传的参数是加密的
3. 路由传参
路由的定义中,要定义参数
参数以 / 的形式加在路由后面
商品id:{item.goods_id}
接收:this.props.match.params.id
注:设置路由参数,当没有传参数时,不会显示组件,解决方法,在对应参数后面写?,
代码如下:
原文链接:http://blog.itblood.com/959.html,转载请注明出处。

![【欧美SLG/汉化/完结】触手怪R4完结 AI版【PC+安卓/5.53G/更新】Tentacular [Release 4]](/wp-content/uploads/replace/2025/11/06/c4b8071e648063b611a66b30e3de0c31.webp)