欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

React路由參數(shù)傳遞與嵌套路由的實(shí)現(xiàn)詳細(xì)講解

 更新時(shí)間:2022年09月28日 15:37:13   作者:月光曬了很涼快  
這篇文章主要介紹了React路由參數(shù)傳遞與嵌套路由的實(shí)現(xiàn),嵌套路由原則是可以無(wú)限嵌套,但是必須要讓使用二級(jí)路由的一級(jí)路由匹配到,否則不顯示,需要的朋友可以參考一下

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狀態(tài)管理Redux原理與介紹

    React狀態(tài)管理Redux原理與介紹

    redux是redux官方react綁定庫(kù),能夠使react組件從redux store中讀取數(shù)據(jù),并且向store分發(fā)actions以此來(lái)更新數(shù)據(jù),這篇文章主要介紹了react-redux的設(shè)置,需要的朋友可以參考下
    2022-08-08
  • React深入了解原理

    React深入了解原理

    React是用于構(gòu)建用戶界面的JavaScript庫(kù),?[1]??起源于Facebook的內(nèi)部項(xiàng)目,該公司對(duì)市場(chǎng)上所有?JavaScript?MVC框架都不滿意,決定自行開(kāi)發(fā)一套,用于架設(shè)Instagram的網(wǎng)站
    2022-07-07
  • react父組件更改props子組件無(wú)法刷新原因及解決方法

    react父組件更改props子組件無(wú)法刷新原因及解決方法

    使用過(guò)vue的朋友都知道,vue父組件更改props的值,子組件是會(huì)刷新的,而react就未必,今天通過(guò)一個(gè)例子給大家介紹react父組件更改props子組件無(wú)法刷新原因,需要的朋友可以參考下
    2022-09-09
  • 簡(jiǎn)談創(chuàng)建React Component的幾種方式

    簡(jiǎn)談創(chuàng)建React Component的幾種方式

    這篇文章主要介紹了創(chuàng)建React Component的幾種方式,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,,需要的朋友可以參考下
    2019-06-06
  • React ref的使用示例

    React ref的使用示例

    這篇文章主要介紹了React ref的使用詳解,幫助大家更好的理解和學(xué)習(xí)使用React,感興趣的朋友可以了解下
    2021-04-04
  • pubsub-js在react中的使用教程

    pubsub-js在react中的使用教程

    pubsub-js?是一個(gè)用于實(shí)現(xiàn)發(fā)布-訂閱模式的 JavaScript 庫(kù),可以用于不同組件之間的通信,在 React 中,可以使用?pubsub-js?來(lái)實(shí)現(xiàn)組件之間的通信,本篇文章給大家講解pubsub-js在react中的使用,感興趣的朋友一起看看吧
    2023-10-10
  • React?antd中setFieldsValu的簡(jiǎn)便使用示例代碼

    React?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-08
  • ahooks useVirtualList 封裝虛擬滾動(dòng)列表

    ahooks useVirtualList 封裝虛擬滾動(dòng)列表

    這篇文章主要為大家介紹了ahooks useVirtualList 封裝虛擬滾動(dòng)列表詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-09-09
  • 淺談在react中如何實(shí)現(xiàn)掃碼槍輸入

    淺談在react中如何實(shí)現(xiàn)掃碼槍輸入

    這篇文章主要介紹了淺談在react中如何實(shí)現(xiàn)掃碼槍輸入,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-07-07
  • 解決react-connect中使用forwardRef遇到的問(wèn)題

    解決react-connect中使用forwardRef遇到的問(wèn)題

    這篇文章主要介紹了解決react-connect中使用forwardRef遇到的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-05-05

最新評(píng)論