React路由

安裝

yarn add react-router-dom

基本路由

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

import { BrowserRouter as Router, Route, Link } from "react-router-dom";

路由組件解釋

Router         總路由

link           路由導(dǎo)航
----to         切換的鏈接

Route          路由
----path       對應(yīng)鏈接
----component  對應(yīng)組件

簡單路由完整代碼

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/1">產(chǎn)品1</Link> | 
<Link to="/produce/abc">產(chǎn)品abc</Link></div>

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ù)對象

出來match對象 還有 history對象和location對象


子路由

我們這里用Navlink 講解它比link 選中的時候多個一個 active 的class

導(dǎo)入Navlink

import {BrowserRouter as Router,Route,Link,NavLink} from 'react-router-dom'

01 定義指令 Link指令

<NavLink to="/detail">詳情</NavLink>

02 定義主路由

<Route path="/detail" component={Detail}></Route>

03 編寫主路由

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)前匹配的主路由地址

04 編寫子路由

function Arg(){
  return (<h1>我是Arg      </h1>);
}

function Com(){
  return (<h1>我是com</h1>);
}

路由404配置 與Switch

Switch能讓匹配的路由唯一

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>

定義404路由要寫到最后

03 編寫404路由

function NoMatch({location}){
   return (<h1>頁面沒有找到{location.pathname}</h1>);
 }

location 當(dāng)前的地址
pathname 當(dāng)了地址信息


重定向

我們需要寫一個默認(rèn)的子路由,這個時候可用重定向

01 導(dǎo)入重定向

import {
 BrowserRouter as Router,
 Route,
 Link,
 Switch,
 NavLink,
 Redirect
} from 'react-router-dom'

重新編寫 主路由

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>);
}

from 是可以省略的


完整代碼

import React, { Component } from 'react';
import Child from './components/Child'
import {BrowserRouter as Router,Route,Link,Switch,NavLink,Redirect} from 'react-router-dom'
// 導(dǎo)入子組件
class App extends Component {
  render() {
    return (
      <div className="App" >    
        <Router>          
          <div>  
            <NavLink to="/" exact>首頁</NavLink> | 
            <NavLink to="/about">關(guān)于</NavLink> |
            <NavLink to="/produce/1">產(chǎn)品1</NavLink> | 
            <NavLink to="/produce/abc">產(chǎn)品abc</NavLink></div>|
            <NavLink to="/detail">詳情</NavLink>
          <hr/>
          <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>
        </Router>  
      </div>
    );
  }
}
function Home(){
  return (<h1>我是首頁</h1>);
}
function About({history}){
  return (<h1>我是關(guān)于 <button onClick={()=>{history.go(-1)}}>返回</button> <button onClick={()=>{history.push("/")}}>首頁</button> </h1>);
}
function Produce({match}){
  return (<h1>我是產(chǎn)品:{match.params.id}</h1>);
}
function NoMatch({location}){
  return (<h1>頁面沒有找到{location.pathname}</h1>);
}
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>);
}
function Arg(){
  return (<h1>我是Arg      </h1>);
}
function Com(){
  return (<h1>我是com</h1>);
}
export default App;

組件的參數(shù)

function About({match,history,location}){
  console.log(match,history,location)
  return (<h1>我是about</h1>);
}

match 匹配的當(dāng)前路由

isExact: true ,//是否精確匹配
params:{}// 當(dāng)前路由的參數(shù)
path:{} //路由指定的path
url:{}// link 指定的鏈接地址

history當(dāng)前路由信息

go() 歷史記錄跳轉(zhuǎn)
goBack() 歷史記錄返回
goFoward() 前進(jìn)
length  歷史記錄的長度
push()  跳轉(zhuǎn) 有歷史記錄
replace() 跳轉(zhuǎn)沒有歷史記錄
location 地址信息
---hash #后面的參數(shù)
---pathname 當(dāng)前路由的地址
---search 問號后面的參數(shù)

location 同history的location 當(dāng)前地址信息

class 編程 js路由跳轉(zhuǎn)

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

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

  • react路由開始今天的話題之前,讓我們先來了解一下前端路由,Ajax誕生以后,解決了每次用戶操作都要向服務(wù)器端發(fā)...
    Daeeman閱讀 1,040評論 0 2
  • 安裝 npm i react-router-dom -S 導(dǎo)入路由需要的組件 import { BrowserRo...
    含含要暴怒閱讀 247評論 0 1
  • 安裝 基本路由 導(dǎo)入路由需要的組件 路由組件解釋 路由的參數(shù) 01 定義指令 Link指令 02 定義路由 03 ...
    耶啵_閱讀 323評論 0 0
  • 安裝 npm i react-router-dom -S導(dǎo)入路由需要的組件import { BrowserRout...
    一只小丫丫閱讀 360評論 0 1
  • 中原區(qū)文藝部。 互相鍛煉同成長,一屆更比一屆強(qiáng)。 乘勝前進(jìn)再努力,團(tuán)結(jié)合作更輝煌。 共同包容同學(xué)習(xí),相互邦扶共成長...
    會說話的石頭閱讀 287評論 0 1

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