zoukankan      html  css  js  c++  java
  • react中路由传参的三种方式

       方式 一

             通过params
            1.路由表中      
                  <Route path=' /sort/:id '   component={Sort}></Route>
               
            2.Link处        
                HTML方式
                     <Link to={ ' /sort/ ' + ' 2 ' }  activeClassName='active'>XXXX</Link>              
               
              JS方式
                    this.props.history.push(  '/sort/'+'2'  )
               
            3.sort页面       
                   通过  this.props.match.params.id        就可以接受到传递过来的参数(id)
               
       方式 二
             通过query
                    前提:必须由其他页面跳过来,参数才会被传递过来
            注:不需要配置路由表。路由表中的内容照常:<Route path='/sort' component={Sort}></Route>
            1.Link处      
             HTML方式
                <Link to={{ pathname: ' /sort ' , query : { name : 'sunny' }}}>
              
           JS方式
                this.props.history.push({ pathname: '/sort' ,query : { name: ' sunny'} })
     
            2.sort页面     
                  this.props.location.query.name
                                    
         方式 三
            通过state
                同query差不多,只是属性不一样,而且state传的参数是加密的,query传的参数是公开的,在地址栏
            1.Link 处      
              HTML方式:
                    <Link to={{ pathname: ' /sort ' , state : { name : 'sunny' }}}> 
                                      
             JS方式:
                this.props.history.push({ pathname:'/sort',state:{name : 'sunny' } })
                                      
            2.sort页面       
                this.props.location.state.name
  • 相关阅读:
    容斥原理算法总结(bzoj 2986 2839)
    网络流系列算法总结(bzoj 3438 1061)
    bzoj 2746: [HEOI2012]旅行问题 AC自动机fail树
    bzoj 3283: 运算器 扩展Baby Step Giant Step && 快速阶乘
    计算几何考场绘图技巧
    bzoj 1845: [Cqoi2005] 三角形面积并 扫描线
    bzoj 3784: 树上的路径 堆维护第k大
    BZOJ 1231: [Usaco2008 Nov]mixup2 混乱的奶牛
    BZOJ 1112: [POI2008]砖块Klo
    BZOJ 1003: [ZJOI2006]物流运输trans DP+最短路
  • 原文地址:https://www.cnblogs.com/ljh12138/p/13566406.html
Copyright © 2011-2022 走看看