码迷,mamicode.com
首页 > 其他好文 > 详细

React Router路由随笔

时间:2019-12-29 23:38:27      阅读:79      评论:0      收藏:0      [点我收藏+]

标签:随笔   function   首页   link   list   css   default   react   home   

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路由随笔

标签:随笔   function   首页   link   list   css   default   react   home   

原文地址:https://www.cnblogs.com/guoyinsheng/p/12116982.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!