React路由參數(shù)傳遞與嵌套路由的實(shí)現(xiàn)詳細(xì)講解
1. 頁(yè)面路由參數(shù)傳遞
1.1 動(dòng)態(tài)路由參數(shù)
描述:
以“/detail/:id”形式傳遞的數(shù)據(jù),在落地組件中通過(guò)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ù)可以傳遞多個(gè) */} <Route path="/detail/:id/:name?" component={Detail} /> <Redirect exact from="/" to="/home" /> <Route component={Notfound} /> </Switch> </div> ) } } export default App
about頁(yè)面:
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
詳情頁(yè)面:
import React, { Component } from 'react' class Detail extends Component { render() { // 獲取動(dòng)態(tài)路由參數(shù) 對(duì)象 let id = this.props.match.params.id || 0 return ( <div> <h3>詳情頁(yè)面 --- {id}</h3> </div> ) } } export default Detail
1.2 search字符串
描述:
通過(guò)地址欄中的 ?key=value&key=value傳遞。
使用:
關(guān)于頁(yè)面:
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
詳情頁(yè)面:
import React, { Component } from 'react' import qs from 'querystring' // 封裝一個(gè)方法,獲取數(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() { // 獲取動(dòng)態(tài)路由參數(shù) 對(duì)象 let id = this.props.match.params.id || 0 // search字符串 字符串 console.log(this.props.location.search) // 將字符串轉(zhuǎn)為對(duì)象 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) // 使用迭代對(duì)象獲取 // for (let [key, value] of search.entries()) { // searchData[key] = value // } // console.log(searchData) // 使用封裝的方法獲取 // console.log(this.props.location.search.toSearch()) return ( <div> <h3>詳情頁(yè)面 --- {id}</h3> </div> ) } } export default Detail
1.3 頁(yè)面參數(shù)隱式傳遞
描述:
隱式傳參(state),通過(guò)地址欄是觀察不到的;通過(guò)路由對(duì)象中的state屬性進(jìn)行數(shù)據(jù)傳遞,在落地組件中通過(guò)this.props.location.state得到。
使用:
home頁(yè)面:
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> {/* 通過(guò)state隱式來(lái)傳遞數(shù)據(jù)到目標(biāo)頁(yè)面 */} <Link to={{ pathname: '/detail', state: { id: 100 }, search: 'name=lisi' }}>去詳情</Link> </div> <hr /> {/* 編程式導(dǎo)航 */} <button onClick={this.jumpDetail}>去詳情頁(yè)</button> </div> ) } } export default Home
詳情頁(yè)面:
import React, { Component } from 'react' class Detail extends Component { render() { // 獲取頁(yè)面隱式傳過(guò)來(lái)的state數(shù)據(jù) 對(duì)象 console.log(this.props.location.state) console.log(this.props.location.search.toSearch()) return ( <div> <h3>詳情頁(yè)面</h3> </div> ) } } export default Detail
tosearch方法(已導(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. 嵌套路由
后臺(tái)首頁(yè):
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() { // 在子路由定義的組件中,可以通過(guò)props中提供的路由對(duì)象來(lái)獲取父路由定義的地址 // let parentRoutePath = this.props.match.path return ( <AdminContainer> <h3>后臺(tái)首頁(yè)</h3> <div> <ul> <li> <NavLink to="/admin/index">后臺(tái)首頁(yè)</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>后臺(tái)首頁(yè)</h3> // <div> // <ul> // <li> // <NavLink to={`${parentRoutePath}/index`}>后臺(tái)首頁(yè)</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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
react父組件更改props子組件無(wú)法刷新原因及解決方法
使用過(guò)vue的朋友都知道,vue父組件更改props的值,子組件是會(huì)刷新的,而react就未必,今天通過(guò)一個(gè)例子給大家介紹react父組件更改props子組件無(wú)法刷新原因,需要的朋友可以參考下2022-09-09簡(jiǎn)談創(chuàng)建React Component的幾種方式
這篇文章主要介紹了創(chuàng)建React Component的幾種方式,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,,需要的朋友可以參考下2019-06-06React?antd中setFieldsValu的簡(jiǎn)便使用示例代碼
form.setFieldsValue是antd?Form組件中的一個(gè)方法,用于動(dòng)態(tài)設(shè)置表單字段的值,它接受一個(gè)對(duì)象作為參數(shù),對(duì)象的鍵是表單字段的名稱,值是要設(shè)置的字段值,這篇文章主要介紹了React?antd中setFieldsValu的簡(jiǎn)便使用,需要的朋友可以參考下2023-08-08ahooks useVirtualList 封裝虛擬滾動(dòng)列表
這篇文章主要為大家介紹了ahooks useVirtualList 封裝虛擬滾動(dòng)列表詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09解決react-connect中使用forwardRef遇到的問(wèn)題
這篇文章主要介紹了解決react-connect中使用forwardRef遇到的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-05-05