react的路由传参

方式 一:经过paramsthis

1.路由表中
  <Route path=' /sort/:id ' component={Sort}></Route>
           
2.Link处 加密

  HTML方式
  <Link to={ ' /sort/ ' + ' 2 ' } activeClassName='active'>XXXX</Link>     
           
  JS方式
  this.props.router.push( '/sort/'+'2' )
           
3.sort页面
  经过 this.props.params.id 就能够接受到传递过来的参数(id)spa

方式 二:经过query
前提:必须由其余页面跳过来,参数才会被传递过来
    注:不须要配置路由表。路由表中的内容照常:<Route path='/sort' component={Sort}></Route>component

1.Link处
  HTML方式
  <Link to={{ pathname : ' /sort ' , query : { name : 'sunny' }}}>
          
JS方式
  this.props.router.push({ path : '/sort' ,query : { name: ' sunny'} })

2.sort页面
  取值:this.props.location.query.name

方式 三:经过state
  同query差很少,只是属性不同,并且state传的参数是加密的,query传的参数是公开的,在地址栏router

1.Link 处
  HTML方式:
  <Link to={{ pathname : ' /sort ' , state : { name : 'sunny' }}}>
  
  JS方式:
  this.props.router.push({ pathname:'/sort',state:{name : 'sunny' } })
  
2.sort页面
  取值:this.props.location.state.name路由

相关文章
相关标签/搜索