React路由參數(shù)傳遞與嵌套路由的實(shí)現(xiàn)詳細(xì)講解
1. 頁面路由參數(shù)傳遞
1.1 動態(tài)路由參數(shù)
描述:
以“/detail/:id”形式傳遞的數(shù)據(jù),在落地組件中通過this.props.match.params得到。
使用:
App.jsx:
import React, { Component } from 'react'
import { Route, Link, NavLink, Switch, Redirect } from 'react-router-dom'
// 匹配成功后渲染的組件
import Home from './views/Home'
import About from './views/About'
import Detail from './views/Detail'
import Notfound from './views/Notfound'
class App extends Component {
render() {
return (
<div>
<h3>App組件</h3>
<div>
<NavLink exact to="/">Home</NavLink>---
<NavLink to="/about">About</NavLink>
</div>
<hr />
<Route path="/home" component={Home} />
<Route path="/about" component={About} />
{/* /detail/1 /detail/2 ... 匹配成功;參數(shù)可以傳遞多個 */}
<Route path="/detail/:id/:name?" component={Detail} />
<Redirect exact from="/" to="/home" />
<Route component={Notfound} />
</Switch>
</div>
)
}
}
export default Appabout頁面:
import React, { Component } from 'react'
import { Link } from 'react-router-dom'
class About extends Component {
render() {
return (
<div>
<h3>關(guān)于我們</h3>
<hr />
<ul>
{Array(10)
.fill('')
.map((_, index) => (
<li key={index}>
<Link to={`/detail/${index}?`}>新聞 -- {index}</Link>
</li>
))}
</ul>
</div>
)
}
}
export default About詳情頁面:
import React, { Component } from 'react'
class Detail extends Component {
render() {
// 獲取動態(tài)路由參數(shù) 對象
let id = this.props.match.params.id || 0
return (
<div>
<h3>詳情頁面 --- {id}</h3>
</div>
)
}
}
export default Detail
1.2 search字符串
描述:
通過地址欄中的 ?key=value&key=value傳遞。
使用:
關(guān)于頁面:
import React, { Component } from 'react'
import { Link } from 'react-router-dom'
class About extends Component {
render() {
return (
<div>
<h3>關(guān)于我們</h3>
<hr />
<ul>
{Array(10)
.fill('')
.map((_, index) => (
<li key={index}>
<Link to={`/detail/${index}?age=${2 + index}`}>新聞 -- {index}</Link>
</li>
))}
</ul>
</div>
)
}
}
export default About詳情頁面:
import React, { Component } from 'react'
import qs from 'querystring'
// 封裝一個方法,獲取數(shù)據(jù)
// String.prototype.toSearch = function () {
// let searchData = {}
// const search = new URLSearchParams(this)
// search.forEach((value, key) => {
// searchData[key] = value
// })
// return searchData
// }
class Detail extends Component {
render() {
// 獲取動態(tài)路由參數(shù) 對象
let id = this.props.match.params.id || 0
// search字符串 字符串
console.log(this.props.location.search)
// 將字符串轉(zhuǎn)為對象
console.log(qs.parse(this.props.location.search.slice(1)))
// 如果用search字符串,推薦用它
const search = new URLSearchParams(this.props.location.search)
// 獲取就字段數(shù)據(jù)
console.log(search.get('age'))
let searchData = {}
search.forEach((value, key) => {
searchData[key] = value
})
console.log(searchData)
// 使用迭代對象獲取
// for (let [key, value] of search.entries()) {
// searchData[key] = value
// }
// console.log(searchData)
// 使用封裝的方法獲取
// console.log(this.props.location.search.toSearch())
return (
<div>
<h3>詳情頁面 --- {id}</h3>
</div>
)
}
}
export default Detail
1.3 頁面參數(shù)隱式傳遞
描述:
隱式傳參(state),通過地址欄是觀察不到的;通過路由對象中的state屬性進(jìn)行數(shù)據(jù)傳遞,在落地組件中通過this.props.location.state得到。
使用:
home頁面:
import React, { Component } from 'react'
import { Link } from 'react-router-dom'
class Home extends Component {
jumpDetail = () => {
this.props.history.push({
pathname: '/detail',
search: 'name=zhangsan',
state: { id: 200 }
})
}
render() {
return (
<div>
<div>
{/* 通過state隱式來傳遞數(shù)據(jù)到目標(biāo)頁面 */}
<Link to={{ pathname: '/detail', state: { id: 100 }, search: 'name=lisi' }}>去詳情</Link>
</div>
<hr />
{/* 編程式導(dǎo)航 */}
<button onClick={this.jumpDetail}>去詳情頁</button>
</div>
)
}
}
export default Home詳情頁面:
import React, { Component } from 'react'
class Detail extends Component {
render() {
// 獲取頁面隱式傳過來的state數(shù)據(jù) 對象
console.log(this.props.location.state)
console.log(this.props.location.search.toSearch())
return (
<div>
<h3>詳情頁面</h3>
</div>
)
}
}
export default Detailtosearch方法(已導(dǎo)入入口文件中,所以可以直接使用):
String.prototype.toSearch = function () {
let searchData = {}
if (this.toString() != '') {
const search = new URLSearchParams(this.toString())
search.forEach((value, key) => {
searchData[key] = value
})
}
return searchData
}
2. 嵌套路由
后臺首頁:
import React, { Component } from 'react'
import { NavLink, Redirect, Route, Switch } from 'react-router-dom'
import { AdminContainer } from './style'
import Index from '../Index'
import User from '../User'
class Admin extends Component {
render() {
// 在子路由定義的組件中,可以通過props中提供的路由對象來獲取父路由定義的地址
// let parentRoutePath = this.props.match.path
return (
<AdminContainer>
<h3>后臺首頁</h3>
<div>
<ul>
<li>
<NavLink to="/admin/index">后臺首頁</NavLink>
</li>
<li>
<NavLink to="/admin/user">用戶列表</NavLink>
</li>
</ul>
<div>
<Switch>
<Route path='/admin/index' component={Index} />
<Route path='/admin/user' component={User} />
<Redirect from='/admin' to='/admin/index' />
</Switch>
</div>
</div>
</AdminContainer>
// <AdminContainer>
// <h3>后臺首頁</h3>
// <div>
// <ul>
// <li>
// <NavLink to={`${parentRoutePath}/index`}>后臺首頁</NavLink>
// </li>
// <li>
// <NavLink to={`${parentRoutePath}/user`}>用戶列表</NavLink>
// </li>
// </ul>
// <div>
// <Switch>
// <Route path={`${parentRoutePath}/index`} component={Index} />
// <Route path={`${parentRoutePath}/user`} component={User} />
// <Redirect exact from={parentRoutePath} to={`${parentRoutePath}/index`} />
// </Switch>
// </div>
// </div>
// </AdminContainer>
)
}
}
export default Admin
到此這篇關(guān)于React路由參數(shù)傳遞與嵌套路由的實(shí)現(xiàn)詳細(xì)講解的文章就介紹到這了,更多相關(guān)React路由參數(shù)傳遞內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
React項(xiàng)目經(jīng)驗(yàn)總結(jié)及遇到的坑
這篇文章主要介紹了React項(xiàng)目經(jīng)驗(yàn)總結(jié),本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-07-07
React組件對子組件children進(jìn)行加強(qiáng)的方法
這篇文章主要給大家介紹了關(guān)于React組件中對子組件children進(jìn)行加強(qiáng)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用React具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06
redux功能強(qiáng)大的Middleware中間件使用學(xué)習(xí)
這篇文章主要為大家介紹了redux功能強(qiáng)大的Middleware中間件使用學(xué)習(xí),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09
React學(xué)習(xí)筆記之高階組件應(yīng)用
這篇文章主要介紹了React 高階組件應(yīng)用,詳細(xì)的介紹了什么是React高階組件和具體使用,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-06-06
React?Hooks中?useRef和useImperativeHandle的使用方式
這篇文章主要介紹了React?Hooks中?useRef和useImperativeHandle的使用方式,文中說明了useRef和useCallback一起使用,?可以解決閉包陷阱的問題,本文結(jié)合實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-10-10
react以create-react-app為基礎(chǔ)創(chuàng)建項(xiàng)目
這篇文章主要介紹了react以create-react-app為基礎(chǔ)創(chuàng)建項(xiàng)目,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-03-03

