React跳轉路由傳參3種方法和區別


1.路由表配置:參數地址欄顯示

<Route path="/list/:id" component={List} />
html:<Link to='/list/2' >跳轉列表頁面</Link>
Js: this.props.history.push('/list/2');
List頁面接收:
console.log(this.props.match.params.id)//傳遞過來的所有參數

 

2.query方法:參數地址欄不顯示,刷新地址欄,參數丟失
html:
<Link to={{ pathname: '/list', query: { name: 'xlf' } }}>跳轉列表頁面</Link>
Js方式:this.props.history.push({ pathname: '/list', query: { name: ' sunny' } })
List頁面接收:
console.log(this.props.location.query.name)//傳遞過來的所有參數

 

3.state方法:參數地址欄不顯示,刷新地址欄,參數不丟失
Html: <Link to={{ pathname: '/list', state: { name: 'xlf' } }}>跳轉列表頁面</Link>
js:this.props.history.push({ pathname: '/list', state: { name: 'sunny' } })
List頁面接收
console.log(this.props.location.state.name)//傳遞過來的所有參數


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM