React中路由傳參及接收參數的方式

from:http://www.javashuo.com/article/p-zdcofizs-hz.htmlhtml

注意: 路由表改變後要重啓服務 this

方式 加密

經過params component

        1.路由表中 router

              <Route path=' /sort/:id '   component={Sort}></Route> htm

        2.Link處 blog

HTML方式 路由

                 <Link to={ ' /sort/ ' + ' 2 ' }  activeClassName='active'>XXXX</Link>           get

JS方式 io

                this.props.router.push(  '/sort/'+'2'  )

        3.sort頁面

經過  this.props.params.id        就能夠接受到傳遞過來的參數(id)

方式

經過query

前提:必須由其餘頁面跳過來,參數纔會被傳遞過來

注:不須要配置路由表。路由表中的內容照常:<Route path='/sort' component={Sort}></Route>

        1.Link處

HTML方式

            <Link to={{ path : ' /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傳的參數是公開的,在地址欄

        1.Link 處

HTML方式:

                <Link to={{ path : ' /sort ' , state : { name : 'sunny' }}}> 

  JS方式:

            this.props.router.push({ pathname:'/sort',state:{name : 'sunny' } })

        2.sort頁面

            this.props.location.state.name

相關文章
相關標籤/搜索