React Router路由随笔

Posted guoyinsheng

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了React Router路由随笔相关的知识,希望对你有一定的参考价值。

1.安装以及基本使用

yarn add react-router-dom

基本结构:

import React from "react";
import { BrowserRouter as Router, Route, Link } from "react-router-dom";
import Index from "./Pages/Index";
import List from "./Pages/List";

//  this.props.history.push("/home/");
function AppRouter() {
  return (
    <Router>
      {/* <ul>
        <li>
          <Link to="/">首页</Link>
        </li>
        <li>
          <Link to="/list/123">列表</Link>
        </li>
      </ul> */}
      <Route path="/" exact component={Index} />
      <Route path="/list/:id" component={List} />
    </Router>
  );
}

export default AppRouter;
import React from "react";
import { BrowserRouter as Router, Route, Link  } from "react-router-dom";
import Index from ‘./Pages/Index‘
import ‘./index.css‘

function AppRouter() {
    return (
      <Router>
          <div className="mainDiv">
            <div className="leftNav">
                <h3>一级导航</h3>
                <ul>
                    <li> <Link to="/">博客首页</Link> </li>
                    <li><Link to="">视频教程</Link> </li>
                    <li><Link to="">职场技能</Link> </li>
                </ul>
            </div>

            <div className="rightMain">
                <Route path="/"  exact component={Index} />
            </div>
          </div>
      </Router>
    );
  }

  export default AppRouter;

二级导航

import React from "react";
import {  Route, Link  } from "react-router-dom";
import Reactpage from ‘./video/ReactPage‘
import Vue from ‘./video/Vue‘
import Flutter from ‘./video/Flutter‘


function Video(){
    return (
        <div>
            <div className="topNav">
                <ul>
                    <li><Link to="/video/reactpage">React教程</Link></li>
                    <li><Link to="/video/vue">Vue教程</Link></li>
                    <li><Link to="/video/flutter">Flutter教程</Link></li>
                </ul>
            </div>
            <div className="videoContent">
                <div><h3>视频教程</h3></div>
                <Route path="/video/reactpage/"   component={Reactpage} />
                <Route path="/video/vue/"   component={Vue} />
                <Route path="/video/flutter/"   component={Flutter} />
            </div>
        </div>
    )
}
export default Video;

二级导航之后一级引入使用

import React from "react";
import { BrowserRouter as Router, Route, Link  } from "react-router-dom";
import Index from ‘./Pages/Index‘
//--关键代码------------start
import Video from ‘./Pages/Video‘
//--关键代码------------end
import ‘./index.css‘

function AppRouter() {
    return (
      <Router>
          <div className="mainDiv">
            <div className="leftNav">
                <h3>一级导航</h3>
                <ul>
                    <li> <Link to="/">博客首页</Link> </li>
                    {/*--关键代码------------start*/}
                    <li><Link to="/video/">视频教程</Link> </li>
                    {/*--关键代码------------end*/}
                    <li><Link to="">职场技能</Link> </li>
                </ul>
            </div>

            <div className="rightMain">
                <Route path="/"  exact component={Index} />
                 {/*--关键代码------------start*/}
                <Route path="/video/"   component={Video} />
                 {/*--关键代码------------end*/}
            </div>
          </div>
      </Router>
    );
  }

  export default AppRouter;

以上是关于React Router路由随笔的主要内容,如果未能解决你的问题,请参考以下文章

route和router 当前与全局路由

简单路由配置--课程内容随笔

如何用react-router做网易云音乐的路由切换

react-router 学习笔记

十八React react-router4.x中:实现路由模块化以及嵌套路由父子组件传值

react-router-dom v^4路由配置