react如何获取路由,以及如何进行路由跳转并传参,以及接收

这篇博客介绍了在React中如何使用`this.props.location.pathname`获取当前路由,使用`this.props.history.push()`进行路由跳转,以及如何传递和接收路由参数。通过示例展示了动态路由`/full:type:phoneno`和`/gen_main:id`等的用法,以及参数封装和解构的方法。此外,还给出了一个包含多个组件路由的`<Router>`配置示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.获取路由 this.props.location.pathname
2.进行路由跳转 this.props.history.push({pathname :"/meet"})
3.获取动态路由参数 this.props.match.params.users

路由传参 this.props.history.push({pathname :"/device/list",query:{data:item})

接收路由传过去的参数 const {data}=this.props.location.query;、

具体实现router.js定义

 <Router>
            <Route exact path="/" component={Login} />
            <Route path="/register" component={Register} />
            <Route path="/forget" component={Forget} />
            <Route path="/full:type:phoneno" component={Full} />
            <Route path="/gen_main:id" component={Gen_Main} />
            <Route path="/service:id" component={Service} />
            <Route path="/adn_main:id" component={Adn_Main} />
            <Route path="/adn_list:id" component={Adn_List} />
            <Route path="/order_info:id" component={OrderInfo} />
            <Route path="/order_detil" component={OrderDetil} />
            <Route path="/cate_rule" component={CateRule} />
            <Route path="/grade_shop:id" component={GradeShop} />
            <Route path="/heart_act:id" component={HeartAct} />
            <Route path="/all_shop:id" component={AllShop} />
            <Route path="/new_shop:id" component={NewShop} />
            <Route path="/cen_shop:id" component={CenShop} />
            <Route path="/user_info:id" component={UserInfo} />
        </Router>

跳转

 if(parseInt(calb.type) === 1){
          this.props.history.push('/gen_main'+id);
        }else{
          this.props.history.push('/adn_main'+id);
        }
       /*也可以将参数封装到一个data里面*/

获取参数

 const userid = this.props.match.params.id;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值