react路由

安裝

npm i react-router-dom -S

導(dǎo)入路由需要的組件

import { BrowserRouter as Router, Route, Link } from "react-router-dom";
【常用路由組件如下:】

  • BrowserRouter as Router
    瀏覽器路由 別名為Router
  • Link導(dǎo)航 to切換的頁面
  • Route路由頁面--path對應(yīng)鏈接component對應(yīng)組件
  • BrowserLink
    帶active激活的class路由連接

簡單路由代碼

import React, { Component } from 'react';
import {BrowserRouter as Router,Route,Link} from 'react-router-dom'
// 導(dǎo)入子組件
class App extends Component {
  render() {
    return (
      <div className="App" >    
        <Router>
          <div>  <Link to="/">首頁</Link> | <Link to="/about">關(guān)于</Link> </div>
          <hr/>
          <Route path="/" exact component={Home}></Route>
          <Route path="/about" component={About}></Route>
        </Router>  
      </div>
    );
  }
}
function Home(){
  return (<h1>我是首頁</h1>);
}
function About(){
  return (<h1>我是關(guān)于</h1>);
}
export default App;

路由的參數(shù)

  • 步驟01:定義指令Link指令
    <Link to="/produce/123">產(chǎn)品123</Link> <Link to="/produce/abc">產(chǎn)品abc</Link>
  • 步驟02:定義路由
    <Route path='/produce/:id' component={Produce}></Route>
  • 步驟03:定義組件-獲取路由參數(shù)
    function Produce({match}){ return (<h1>我是產(chǎn)品:{match.params.id}</h1>); }
    [match 是組件默認(rèn)傳遞的參數(shù), match.paras 組件路由參數(shù)對象, 還有 history對象和location對象]

子路由

  • 步驟01:導(dǎo)入NavLink
    import {BrowserRouter as Router,Route,Link,NavLink} from 'react-router-dom'
  • 步驟02:定義指令NavLink
    <NavLink to="/detail">詳情</NavLink>
  • 步驟03:定義主路由
    <Route path="/detail" component={Detail}></Route>
  • 步驟04:編寫主路由
function Detail({location,match}){
  return (
    <div>
      <div><NavLink to={match.url+'/arg'}>參數(shù)</NavLink> | <NavLink to={match.url+'/com'}>評論</NavLink> | </div>
      <hr/>
      <Route path={match.url+'/arg'} component={Arg}></Route>
      <Route path={match.url+'/com'} component={Com}></Route>
    </div>);
}

[match.url 獲取當(dāng)前匹配的主路由地址]

  • 步驟05:編寫子路由
function Arg(){
  return (<h1>我是Arg      </h1>);
}
function Com(){
  return (<h1>我是com</h1>);
}

404頁面配置和Switch組件

[Switch能讓匹配的路由唯一]
[定義404,要寫在最后]
[location 當(dāng)前的地址]
[pathname 地址欄的信息]

  • 步驟01:導(dǎo)入Switch
    import {BrowserRouter as Router,Route,Link,Switch,NavLink,Redirect} from 'react-router-dom'
  • 步驟02:定義路由
<Switch>
  <Route path="/" exact component={Home}></Route>
  <Route path="/about" component={About}></Route>
  <Route path="/produce/:id" component={Produce}></Route>
  <Route path="/detail" component={Detail}></Route>
  <Route component={NoMatch}></Route>
</Switch>
  • 步驟03:編寫404路由
    function NoMatch({location}){ return (<h1>頁面沒有找到{location.pathname}</h1>); }

路由重定向

  • 導(dǎo)入重定向組件Redirect
  • 重新編寫主路由
function Detail({location,match}){
  return (<div>
      <div><NavLink to={match.url+'/arg'}>參數(shù)</NavLink> | <NavLink to={match.url+'/com'}>評論</NavLink> | </div>
      <hr/>
      <Switch>
      <Route path={match.url+'/arg'} component={Arg}></Route>
      <Route path={match.url+'/com'} component={Com}></Route>      
      <Redirect from={match.url} to={match.url+'/arg'}/>
      </Switch>
    </div>);
}

路由組件的參數(shù)

  • history

goBack() 返回
goForward() 前進(jìn)
push('/') 切換跳轉(zhuǎn)
replace 跳轉(zhuǎn)不留歷史記錄

  • location 地址欄信息

pathname路徑

  • match

params路由參數(shù)
url 配置的地址

路由如何傳參

props

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 路由 正常的組件內(nèi)部是沒有路由對象的 被路由處理過的組件才有路由對象 BrowserRouter HashRout...
    jie_han閱讀 305評論 0 0
  • React中JSX原理 在vue中,我們使用render函數(shù)來構(gòu)建組件的dom結(jié)構(gòu)性能較高,因為省去了查找和編譯模...
    learninginto閱讀 2,099評論 0 5
  • 版權(quán)聲明:本文為博主原創(chuàng)文章,未經(jīng)博主允許不得轉(zhuǎn)載。 PS:轉(zhuǎn)載請注明出處作者:TigerChain地址:http...
    TigerChain閱讀 23,071評論 3 49
  • 猶記昨日剛?cè)肭?,漫山紅葉隨風(fēng)休; 而今飛雪迎風(fēng)落,夢醒方知已別秋; “2002年得第一場雪比以往來的更晚一些”伴隨...
    A今夜有風(fēng)閱讀 999評論 0 2

友情鏈接更多精彩內(nèi)容