react-router v4 參數傳遞

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

   方式 一:加密

         經過paramscomponent

        1.路由表中      路由

              <Route path=' /user/:id '   component={User}></Route>hash

           

        2.Link處        io

            HTML方式配置

                 <Link to={ pathname:' /user/2 ',query:{name:jack},state:{value:123},search:'?sort=name',hash:'#the-hash',abc:'def'}  activeClassName='active'>XXXX</Link>              sort

           

          JS方式query

                this.props.history.push( { pathname:' /user/2 ',query:{name:jack},state:{value:123},search:'?sort=name',hash:'#the-hash',abc:'def'})history

           

        3.user頁面       

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

           

   方式 二:

         經過query

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

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

        1.Link處      

         HTML方式

            <Link to={{ pathname:' /user',query:{name:jack},state:{value:123},search:'?sort=name',hash:'#the-hash',abc:'def'}}>

          

       JS方式

            this.props.history.push({ pathname:' /user',query:{name:jack},state:{value:123},search:'?sort=name',hash:'#the-hash',abc:'def'})

 

        2.user頁面     

              this.props.location.query.name //jack

              this.props.location.state.value  //123

              this.props.location.search  //?sort=name

              this.props.location.hash  //#the-hash

              this.props.location.abc  //def  (自定義參數)

                                

     方式 三:

        經過state

            同query差很少,只是屬性不同,state傳的參數是加密的

        1.Link 處      

          HTML方式:

                <Link to={{ pathname:' /user',query:{name:jack},state:{value:123},search:'?sort=name',hash:'#the-hash',abc:'def'}}> 

                                  

         JS方式:

            this.props.history.push({ pathname:' /user',query:{name:jack},state:{value:123},search:'?sort=name',hash:'#the-hash',abc:'def'})

                                  

        2.user頁面       

            this.props.location.state.value

相關文章
相關標籤/搜索