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

注意:   路由表改變後要重啓服務  
    方式 一
         經過params
        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)
           
    方式 二
          經過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
                                  
相關文章
相關標籤/搜索