react 路由传参
方式 一:
经过params
1.路由表中
<Route path=' /test/:id ' component={test}></Route>
2.Link处
HTML方式
<Link to={ ' /test/ ' + ' 2 ' } activeClassName='active'>XXXX</Link>
JS方式
this.props.history.push( '/user/'+'2' )
3.user页面
经过 this.props.match.params.id 就能够接受到传递过来的参数(id)
方式 二:
经过query
前提:必须由其余页面跳过来,参数才会被传递过来
注:不须要配置路由表。路由表中的内容照常:<Route path='/test' component={test}></Route>
1.Link处
HTML方式
<Link to={{ pathname: ' /test' , query : {num:'1234' }}}>
JS方式
this.props.history.push({ pathname : '/test' ,query : { num: '1234'} })react
2.user页面
this.props.location.query.num//建议不用 刷新页面时 丢失
方式 三:
经过state
同query差很少,只是属性不同,并且state传的参数是加密的,query传的参数是公开的,在地址栏
1.Link 处
HTML方式:
<Link to={{ pathname : ' /test' , state : { num: '1234' }}}>
JS方式:
this.props.history.push({ pathname:'/test',state:{num: '1234' } })
2.子组件页面
this.props.location.state.num 能够拿到传过来的数据,建议使用该方法,传过来的数据不会出如今地址栏,刷新页面数据不会丢失
this