react路由跳轉的幾種方式是什麼

WBOY
發布: 2022-04-21 11:05:00
原創
3936 人瀏覽過

方式:1、利用params,參數會顯示在網址列,語法「...({pathname:...,search:網址列資料})」;2、利用state,網址列看不到數據,語法「...({pathname:...,state:{test:...}}」。

react路由跳轉的幾種方式是什麼

本教學操作環境:Windows10系統、react17.0.1版、Dell G3電腦。

react路由跳轉的幾種方式是什麼

注意: 這裡使用的 react-router-dom是版本5以上,路由形式是history模式
#react-router-dom文件位址,其中依賴套件history的github位址

1. params形式,路由跳轉後,參數會顯示在網址列

react路由跳轉的幾種方式是什麼

    ##跳轉的方法是使用
  • history.push({pathname: '/personal', search: 'test=22222'}),其中search#鍵對應的值就是拼接在網址列的資料<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">import React from 'react'import { useHistory } from 'react-router-dom'export default ()=&gt; { const history = useHistory() // 页面跳转方法 history.push({pathname: '/personal', search: 'test=22222'}) return 123}</pre><div class="contentsignin">登入後複製</div></div>
  • 接收的方法。資料都是儲存在
  • useLocation中的search取得<div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">import React from 'react'import { useLocation } from 'react-router-dom'export default ()=&gt; { const location = useLocation() // 页面跳转方法 console.log(location, 'props') return 123}</pre><div class="contentsignin">登入後複製</div></div><div class="contentsignin">登入後複製</div></div> react路由跳轉的幾種方式是什麼

#2. 使用state的形式,頁面刷新不會遺失數據,並且網址列也看不到數據

##跳轉的方法是使用
    history.push({pathname: '/personal', state: {test: 'dashboard'}})
  • ,其中search鍵對應的值就是拼接在位址欄的資料
    import React from 'react'import { useHistory } from 'react-router-dom'export default ()=> {
    	const history = useHistory()
    	// 页面跳转方法
    	history.push({pathname: '/personal', state: { test: 'dashboard' }})
    	return 123}
    登入後複製
    接收的方法。資料都是儲存在
  • useLocation
  • 中的search取得<div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">import React from 'react'import { useLocation } from 'react-router-dom'export default ()=&gt; { const location = useLocation() // 页面跳转方法 console.log(location, 'props') return 123}</pre><div class="contentsignin">登入後複製</div></div><div class="contentsignin">登入後複製</div></div> react路由跳轉的幾種方式是什麼##推薦學習:《
  • react影片教學

以上是react路由跳轉的幾種方式是什麼的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新問題
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!