• [尚硅谷React笔记]——第5章 React 路由


    目录:

    1. 对SPA应用的理解
    2. 对路由的理解
    3. 前端路由原理
    4. 路由的基本使用
    5. 路由组件与一般组件
    6. NavLink的使用
    7. 封装NavLink组件
    8. Switch的使用
    9. 解决样式丢失问题
    10. 路由的模糊匹配与严格匹配
    11. Redirect的使用
    12. 嵌套路由
    13. 向路由组件传递params参数
    14. 向路由组件传递search参数
    15. .向路由组件传递state参数
    16. 总结路由参数
    17. push与repalce
    18. 编程式路由导航
    19. withRouter的作用
    20. BrowserRouter与HashRouter

    1.对SPA应用的理解

    1. 单页Web应用(single page web application,SPA)。
    2. 整个应用只有一个完整的页面
    3. 点击页面中的链接不会刷新页面,只会做页面的局部更新。
    4. 数据都需要通过ajax请求获取, 并在前端异步展现。

    2.对路由的理解 

    什么是路由?

    1. 一个路由就是一个映射关系(key:value)
    2. key为路径, value可能是function或component

    路由分类

    1. 后端路由:
      1. 理解: value是function, 用来处理客户端提交的请求。
      2. 注册路由: router.get(path, function(req, res))
      3. 工作过程:当node接收到一个请求时, 根据请求路径找到匹配的路由, 调用路由中的函数来处理请求, 返回响应数据
    2. 前端路由:
      1. 浏览器端路由,value是component,用于展示页面内容。
      2. 注册路由:
      3. 工作过程:当浏览器的path变为/test时, 当前路由组件就会变为Test组件

    3.前端路由原理

    前端路由的基石.html 

    1. "en">
    2. <head>
    3. "UTF-8">
    4. 前端路由的基石_history




     4.路由的基本使用

    1. 明确好界面中的导航区、展示区
    2. 导航区的a标签改为Link标签 Demo
    3. 展示区写Route标签进行路径的匹配
    4. 的最外侧包裹了一个

    App.js

    1. import React, {Component} from 'react';
    2. import {Link, Route} from "react-router-dom";
    3. import About from "./components/About/About";
    4. import Home from "./components/Home/Home";
    5. class App extends Component {
    6. render() {
    7. return (
    8. "row">
    9. "col-xs-offset-2 col-xs-8">
    10. "page-header">

      React Router Demo

  • "row">
  • "col-xs-2 col-xs-offset-2">
  • "list-group">
  • "list-group-item" to="/about">About
  • "list-group-item" to="/home">Home
  • "col-xs-6">
  • "panel">
  • "panel-body">
  • "/about" component={About}>
  • "/home" component={Home}>
  • );
  • }
  • }
  • export default App;
  • index.js

    1. import React from 'react';
    2. import ReactDOM from 'react-dom/client';
    3. import App from './App';
    4. import {BrowserRouter} from "react-router-dom";
    5. const root = ReactDOM.createRoot(document.getElementById('root'));
    6. root.render(
    7. );

    About.jsx

    1. import React, {Component} from 'react';
    2. class About extends Component {
    3. render() {
    4. return (
    5. 我是About的内容

    6. );
    7. }
    8. }
    9. export default About;

    Home.jsx

    1. import React, {Component} from 'react';
    2. class Home extends Component {
    3. render() {
    4. return (
    5. 我是Home的内容

    6. );
    7. }
    8. }
    9. export default Home;

    运行结果:

    5.路由组件与一般组件

    1. 写法不同:
      1. 一般组件:
      2. 路由组件:
    2. 存放位置不同:
      1. 一般组件:components
      2. 路由组件: pages
    3. 接收到的props不同:
      1. 一般组件:,写组件标签时传递了什么,就能收到什么
      2. 路由组件:接收到三个固定的属性
        1. history:
           
          1. go: f go(n)
          2. goBack: f goBack()
          3. goForward: f goForward()
          4. push: f push(path,state)
          5. replace: f replace(path, state)
        2. location:

          1. pathname: "/about"
          2. search: ""
          3. state: undefined
        3. match:|

          1. params: {}
          2. path: "/about"
          3. url: "/about"

     6.NavLink的使用

     Header.jsx

    1. import React, {Component} from 'react';
    2. class Header extends Component {
    3. render() {
    4. // console.log('Header组件收到的props是', this.props)
    5. return (
    6. "page-header">

      React Router Demo

    7. );
    8. }
    9. }
    10. export default Header;

    About.jsx

    1. import React, {Component} from 'react';
    2. class About extends Component {
    3. render() {
    4. // console.log('About组件收到的props是', this.props)
    5. return (
    6. 我是About的内容

    7. );
    8. }
    9. }
    10. export default About;

    Home.jsx

    1. import React, {Component} from 'react';
    2. class Home extends Component {
    3. render() {
    4. return (
    5. 我是Home的内容

    6. );
    7. }
    8. }
    9. export default Home;

    App.js

    1. import React, {Component} from 'react';
    2. import {NavLink, Route} from "react-router-dom";
    3. import About from "./pages/About/About";
    4. import Home from "./pages/Home/Home";
    5. import Header from "./components/Header/Header";
    6. class App extends Component {
    7. render() {
    8. return (
    9. "row">
    10. "col-xs-offset-2 col-xs-8">
    11. "row">
    12. "col-xs-2 col-xs-offset-2">
    13. "list-group">
    14. "atguigu" className="list-group-item" to="/about">About
    15. "atguigu" className="list-group-item" to="/home">Home
    16. "col-xs-6">
    17. "panel">
    18. "panel-body">
    19. "/about" component={About}>
    20. "/home" component={Home}>
    21. );
    22. }
    23. }
    24. export default App;

    index.js

    1. import React from 'react';
    2. import ReactDOM from 'react-dom/client';
    3. import App from './App';
    4. import {BrowserRouter} from "react-router-dom";
    5. const root = ReactDOM.createRoot(document.getElementById('root'));
    6. root.render(
    7. );

    index.html

    1. "en">
    2. <head>
    3. "utf-8"/>
    4. <link rel="icon" href="%PUBLIC_URL%/favicon.ico"/>
    5. "viewport" content="width=device-width, initial-scale=1"/>
    6. "theme-color" content="#000000"/>
    7. name="description"
    8. content="Web site created using create-react-app"
    9. />
    10. <link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png"/>
    11. <link rel="manifest" href="%PUBLIC_URL%/manifest.json"/>
    12. <link rel="stylesheet" href="css/bootstrap.css">
    13. React App
    14. id="root">

    项目结构:

    运行结果:

     7.封装NavLink组件

    NavLink与封装NavLink

    1. NavLink可以实现路由链接的高亮,通过activeclassName指定样式名
    2. 标签体内容是一个特殊的标签属性
    3. 通过this.props.children可以获取标签体内容

     App.js

    1. import React, {Component} from 'react';
    2. import {NavLink, Route} from "react-router-dom";
    3. import About from "./pages/About/About";
    4. import Home from "./pages/Home/Home";
    5. import Header from "./components/Header/Header";
    6. import MyNavLink from "./components/MyNavLink/MyNavLink";
    7. class App extends Component {
    8. render() {
    9. return (
    10. "row">
    11. "col-xs-offset-2 col-xs-8">
    12. "row">
    13. "col-xs-2 col-xs-offset-2">
    14. "list-group">
    15. "/about" a={1} b={2} c={3}>About
    16. "/home">Home
    17. "col-xs-6">
    18. "panel">
    19. "panel-body">
    20. "/about" component={About}>
    21. "/home" component={Home}>
    22. );
    23. }
    24. }
    25. export default App;

    index.js

    1. import React from 'react';
    2. import ReactDOM from 'react-dom/client';
    3. import App from './App';
    4. import {BrowserRouter} from "react-router-dom";
    5. const root = ReactDOM.createRoot(document.getElementById('root'));
    6. root.render(
    7. );

    Header.jsx

    1. import React, {Component} from 'react';
    2. class Header extends Component {
    3. render() {
    4. // console.log('Header组件收到的props是', this.props)
    5. return (
    6. "page-header">

      React Router Demo

    7. );
    8. }
    9. }
    10. export default Header;

    MyNavLink.jsx

    1. import React, {Component} from 'react';
    2. import {NavLink} from "react-router-dom";
    3. class MyNavLink extends Component {
    4. render() {
    5. console.log(this.props)
    6. return (
    7. "atguigu" className="list-group-item" {...this.props}/>
    8. );
    9. }
    10. }
    11. export default MyNavLink;

    About.jsx

    1. import React, {Component} from 'react';
    2. class About extends Component {
    3. render() {
    4. // console.log('About组件收到的props是', this.props)
    5. return (
    6. 我是About的内容

    7. );
    8. }
    9. }
    10. export default About;

    Home.jsx

    1. import React, {Component} from 'react';
    2. class Home extends Component {
    3. render() {
    4. return (
    5. 我是Home的内容

    6. );
    7. }
    8. }
    9. export default Home;

    项目结构:

    运行结果:

     

    8.Switch的使用

    1. 通常情况下,path和component是一一对应的关系。
    2. Switch可以提高路由匹配效率(单一匹配)。

    Test.jsx

    1. import React, {Component} from 'react';
    2. class Test extends Component {
    3. render() {
    4. return (
    5. Test...

    6. );
    7. }
    8. }
    9. export default Test;

    App.js

    1. import React, {Component} from 'react';
    2. import {NavLink, Route, Switch} from "react-router-dom";
    3. import About from "./pages/About/About";
    4. import Home from "./pages/Home/Home";
    5. import Header from "./components/Header/Header";
    6. import MyNavLink from "./components/MyNavLink/MyNavLink";
    7. import Test from "./pages/Test/Test";
    8. class App extends Component {
    9. render() {
    10. return (
    11. "row">
    12. "col-xs-offset-2 col-xs-8">
    13. "row">
    14. "col-xs-2 col-xs-offset-2">
    15. "list-group">
    16. "/about" a={1} b={2} c={3}>About
    17. "/home">Home
    18. "col-xs-6">
    19. "panel">
    20. "panel-body">
    21. "/about" component={About}>
    22. "/home" component={Home}>
    23. "/home" component={Test}>
    24. );
    25. }
    26. }
    27. export default App;

    运行结果:

    项目结构:

    9.解决样式丢失问题 

    1. public/index.html 中引入样式时不写./写/(常用)

    2. public/index.html 中引入样式时不写﹒/写%PUBLIC_URL%(常用)

    3. 使用HashRouter

    1../css/bootstrap.css改为:

    <link rel="stylesheet" href="css/bootstrap.css">

    2../css/bootstrap.css改为:

    <link rel="stylesheet" href="%PUBLIC_URL%/css/bootstrap.css">

    3.BroserRouter改成HashRouter

    1. import React from 'react';
    2. import ReactDOM from 'react-dom/client';
    3. import App from './App';
    4. import {BrowserRouter, HashRouter} from "react-router-dom";
    5. const root = ReactDOM.createRoot(document.getElementById('root'));
    6. root.render(
    7. );

     10.路由的模糊匹配与严格匹配

    1. 默认使用的是模糊匹配(简单记:【输入的路径】必须包含要【匹配的路径】,且顺序要一致)
    2. 开启严格匹配:
    3. 严格匹配不要随便开启,需要再开,有些时候开启会导致无法继续匹配二级路由

     App.js

    1. import React, {Component} from 'react';
    2. import {Route, Switch} from "react-router-dom";
    3. import About from "./pages/About/About";
    4. import Home from "./pages/Home/Home";
    5. import Header from "./components/Header/Header";
    6. import MyNavLink from "./components/MyNavLink/MyNavLink";
    7. class App extends Component {
    8. render() {
    9. return (
    10. "row">
    11. "col-xs-offset-2 col-xs-8">
    12. "row">
    13. "col-xs-2 col-xs-offset-2">
    14. "list-group">
    15. "/about">About
    16. "/home/a/b">Home
    17. "col-xs-6">
    18. "panel">
    19. "panel-body">
    20. "/about" component={About}>
    21. "/home" component={Home}>
    22. );
    23. }
    24. }
    25. export default App;

    运行结果:

    11.Redirect的使用

    1. 一般写在所有路由注册的最下方,当所有路由都无法匹配时,跳转到Redirect指定的路由
    2. 具体编码:
      1. "/about" component={About}/>
      2. "/home" component={Home}/>
      3. "/about"/>

    App.js

    1. import React, {Component} from 'react';
    2. import {Redirect, Route, Switch} from "react-router-dom";
    3. import About from "./pages/About/About";
    4. import Home from "./pages/Home/Home";
    5. import Header from "./components/Header/Header";
    6. import MyNavLink from "./components/MyNavLink/MyNavLink";
    7. class App extends Component {
    8. render() {
    9. return (
    10. "row">
    11. "col-xs-offset-2 col-xs-8">
    12. "row">
    13. "col-xs-2 col-xs-offset-2">
    14. "list-group">
    15. "/about">About
    16. "/home">Home
    17. "col-xs-6">
    18. "panel">
    19. "panel-body">
    20. "/about" component={About}>
    21. "/home" component={Home}>
    22. "/about">
    23. );
    24. }
    25. }
    26. export default App;

    12.嵌套路由

    1. 注册子路由时要写上父路由的path值
    2. 路由的匹配是按照注册路由的顺序进行的

    App.js

    1. import React, {Component} from 'react';
    2. import {Redirect, Route, Switch} from "react-router-dom";
    3. import About from "./pages/About/About";
    4. import Home from "./pages/Home/Home";
    5. import Header from "./components/Header/Header";
    6. import MyNavLink from "./components/MyNavLink/MyNavLink";
    7. class App extends Component {
    8. render() {
    9. return (
    10. "row">
    11. "col-xs-offset-2 col-xs-8">
    12. "row">
    13. "col-xs-2 col-xs-offset-2">
    14. "list-group">
    15. "/about">About
    16. "/home">Home
    17. "col-xs-6">
    18. "panel">
    19. "panel-body">
    20. "/about" component={About}>
    21. "/home" component={Home}>
    22. "/about">
    23. );
    24. }
    25. }
    26. export default App;

    Message.jsx

    1. import React, {Component} from 'react';
    2. class Message extends Component {
    3. render() {
    4. return (
      • );
      • }
      • }
      • export default Message;

       News.jsx

      1. import React, {Component} from 'react';
      2. class News extends Component {
      3. render() {
      4. return (
        • news001
        • news002
        • news003
        • );
        • }
        • }
        • export default News;

        Home.jsx

        1. import React, {Component} from 'react';
        2. import MyNavLink from "../../components/MyNavLink/MyNavLink";
        3. import {Redirect, Route, Switch} from "react-router-dom";
        4. import News from "./News/News";
        5. import Message from "./Message/Message";
        6. class Home extends Component {
        7. render() {
        8. return (
        9. Home组件内容

          • "nav nav-tabs">
        10. "/home/news">News
        11. "/home/message">Message
        12. "/home/news" component={News}>
        13. "/home/message" component={Message}>
        14. "/home/news">
        15. );
        16. }
        17. }
        18. export default Home;

        MyNavLink.jsx

        1. import React, {Component} from 'react';
        2. import {NavLink} from "react-router-dom";
        3. class MyNavLink extends Component {
        4. render() {
        5. console.log(this.props)
        6. return (
        7. "atguigu" className="list-group-item" {...this.props}/>
        8. );
        9. }
        10. }
        11. export default MyNavLink;

        Header.jsx

        1. import React, {Component} from 'react';
        2. class Header extends Component {
        3. render() {
        4. // console.log('Header组件收到的props是', this.props)
        5. return (
        6. "page-header">

          React Router Demo

        7. );
        8. }
        9. }
        10. export default Header;

        About.jsx

        1. import React, {Component} from 'react';
        2. class About extends Component {
        3. render() {
        4. // console.log('About组件收到的props是', this.props)
        5. return (
        6. 我是About的内容

        7. );
        8. }
        9. }
        10. export default About;

        index.js

        1. import React from 'react';
        2. import ReactDOM from 'react-dom/client';
        3. import App from './App';
        4. import {BrowserRouter, HashRouter} from "react-router-dom";
        5. const root = ReactDOM.createRoot(document.getElementById('root'));
        6. root.render(
        7. );

        项目结构:

        运行结果:

        13. 向路由组件传递params参数

        1.params参数

        Header.jsx

        1. import React, {Component} from 'react';
        2. class Header extends Component {
        3. render() {
        4. // console.log('Header组件收到的props是', this.props)
        5. return (
        6. "page-header">

          React Router Demo

        7. );
        8. }
        9. }
        10. export default Header;

        MyNavLink.jsx

        1. import React, {Component} from 'react';
        2. import {NavLink} from "react-router-dom";
        3. class MyNavLink extends Component {
        4. render() {
        5. console.log(this.props)
        6. return (
        7. "atguigu" className="list-group-item" {...this.props}/>
        8. );
        9. }
        10. }
        11. export default MyNavLink;

        About.jsx

        1. import React, {Component} from 'react';
        2. class About extends Component {
        3. render() {
        4. // console.log('About组件收到的props是', this.props)
        5. return (
        6. 我是About的内容

        7. );
        8. }
        9. }
        10. export default About;

        Detail.jsx

        1. import React, {Component} from 'react';
        2. const DetailData = [
        3. {id: '01', content: '你好,中国'},
        4. {id: '02', content: '你好,尚硅谷'},
        5. {id: '03', content: '你好,未来的自己'},
        6. ]
        7. class Detail extends Component {
        8. render() {
        9. // console.log(this.props)
        10. const {id, title} = this.props.match.params
        11. const findResult = DetailData.find((detailObj) => {
        12. return detailObj.id === id
        13. })
        14. return (
          • ID:{id}
          • TITLE:{title}
          • CONTENT:{findResult.content}
          • );
          • }
          • }
          • export default Detail;

          Message.jsx

          1. import React, {Component} from 'react';
          2. import Detail from "./Detail/Detail";
          3. import {Link, Route} from "react-router-dom";
          4. class Message extends Component {
          5. state = {
          6. messageArr: [
          7. {id: '01', title: '消息1'},
          8. {id: '02', title: '消息2'},
          9. {id: '03', title: '消息3'},
          10. ]
          11. }
          12. render() {
          13. const {messageArr} = this.state
          14. return (
            • {
            • messageArr.map((msgObj) => {
            • return (
            • ${msgObj.id}/${msgObj.title}`}>{msgObj.title}
            • )
            • })
            • }

            • );
            • }
            • }
            • export default Message;

            News.jsx

            1. import React, {Component} from 'react';
            2. class News extends Component {
            3. render() {
            4. return (
              • news001
              • news002
              • news003
              • );
              • }
              • }
              • export default News;

              Home.jsx 

              1. import React, {Component} from 'react';
              2. import MyNavLink from "../../components/MyNavLink/MyNavLink";
              3. import {Redirect, Route, Switch} from "react-router-dom";
              4. import News from "./News/News";
              5. import Message from "./Message/Message";
              6. class Home extends Component {
              7. render() {
              8. return (
              9. Home组件内容

                • "nav nav-tabs">
              10. "/home/news">News
              11. "/home/message">Message
              12. "/home/news" component={News}>
              13. "/home/message" component={Message}>
              14. "/home/news">
              15. );
              16. }
              17. }
              18. export default Home;

              App.js

              1. import React, {Component} from 'react';
              2. import {Redirect, Route, Switch} from "react-router-dom";
              3. import About from "./pages/About/About";
              4. import Home from "./pages/Home/Home";
              5. import Header from "./components/Header/Header";
              6. import MyNavLink from "./components/MyNavLink/MyNavLink";
              7. class App extends Component {
              8. render() {
              9. return (
              10. "row">
              11. "col-xs-offset-2 col-xs-8">
              12. "row">
              13. "col-xs-2 col-xs-offset-2">
              14. "list-group">
              15. "/about">About
              16. "/home">Home
              17. "col-xs-6">
              18. "panel">
              19. "panel-body">
              20. "/about" component={About}>
              21. "/home" component={Home}>
              22. "/about">
              23. );
              24. }
              25. }
              26. export default App;

              index.js

              1. import React from 'react';
              2. import ReactDOM from 'react-dom/client';
              3. import App from './App';
              4. import {BrowserRouter, HashRouter} from "react-router-dom";
              5. const root = ReactDOM.createRoot(document.getElementById('root'));
              6. root.render(
              7. );

              运行结果:

              项目结构:

               

              14.向路由组件传递search参数

              2.search参数

              Message.jsx

              1. import React, {Component} from 'react';
              2. import Detail from "./Detail/Detail";
              3. import {Link, Route} from "react-router-dom";
              4. class Message extends Component {
              5. state = {
              6. messageArr: [
              7. {id: '01', title: '消息1'},
              8. {id: '02', title: '消息2'},
              9. {id: '03', title: '消息3'},
              10. ]
              11. }
              12. render() {
              13. const {messageArr} = this.state
              14. return (
                • {
                • messageArr.map((msgObj) => {
                • return (
                • {/*${msgObj.id}/${msgObj.title}`}>{msgObj.title}*/}
                • ${msgObj.id}&title=${msgObj.title}`}>{msgObj.title}
                • )
                • })
                • }

                • {/**/}
                • );
                • }
                • }
                • export default Message;

                Detail.jsx

                1. import React, {Component} from 'react';
                2. import qs from "qs"
                3. const DetailData = [
                4. {id: '01', content: '你好,中国'},
                5. {id: '02', content: '你好,尚硅谷'},
                6. {id: '03', content: '你好,未来的自己'},
                7. ]
                8. class Detail extends Component {
                9. render() {
                10. console.log(this.props)
                11. // const {id, title} = this.props.match.params
                12. const {search} = this.props.location
                13. const {id, title} = qs.parse(search.slice(1))
                14. const findResult = DetailData.find((detailObj) => {
                15. return detailObj.id === id
                16. })
                17. return (
                  • ID:{id}
                  • TITLE:{title}
                  • CONTENT:{findResult.content}
                  • );
                  • }
                  • }
                  • export default Detail;

                  运行结果:

                  项目结构:

                  15.向路由组件传递state参数

                  state参数:

                   Message.jsx

                  1. import React, {Component} from 'react';
                  2. import Detail from "./Detail/Detail";
                  3. import {Link, Route} from "react-router-dom";
                  4. class Message extends Component {
                  5. state = {
                  6. messageArr: [
                  7. {id: '01', title: '消息1'},
                  8. {id: '02', title: '消息2'},
                  9. {id: '03', title: '消息3'},
                  10. ]
                  11. }
                  12. render() {
                  13. const {messageArr} = this.state
                  14. return (
                    • {
                    • messageArr.map((msgObj) => {
                    • return (
                    • {/*${msgObj.id}/${msgObj.title}`}>{msgObj.title}*/}
                    • {/*${msgObj.id}&title=${msgObj.title}`}>{msgObj.title}*/}
                    • pathname: '/home/message/detail',
                    • state: {id: msgObj.id, title: msgObj.title}
                    • }}>{msgObj.title}
                    • )
                    • })
                    • }

                    • {/**/}
                    • {/**/}
                    • );
                    • }
                    • }
                    • export default Message;

                    Detail.jsx

                    1. import React, {Component} from 'react';
                    2. import qs from "qs"
                    3. const DetailData = [
                    4. {id: '01', content: '你好,中国'},
                    5. {id: '02', content: '你好,尚硅谷'},
                    6. {id: '03', content: '你好,未来的自己'},
                    7. ]
                    8. class Detail extends Component {
                    9. render() {
                    10. console.log(this.props)
                    11. // const {id, title} = this.props.match.params
                    12. // const {search} = this.props.location
                    13. // const {id, title} = qs.parse(search.slice(1))
                    14. const {id, title} = this.props.location.state || {}
                    15. const findResult = DetailData.find((detailObj) => {
                    16. return detailObj.id === id
                    17. }) || {}
                    18. return (
                      • ID:{id}
                      • TITLE:{title}
                      • CONTENT:{findResult.content}
                      • );
                      • }
                      • }
                      • export default Detail;

                      运行结果:

                      16.总结路由参数

                      17.push与repalce 

                      Message.jsx

                      1. import React, {Component} from 'react';
                      2. import Detail from "./Detail/Detail";
                      3. import {Link, Route} from "react-router-dom";
                      4. class Message extends Component {
                      5. state = {
                      6. messageArr: [
                      7. {id: '01', title: '消息1'},
                      8. {id: '02', title: '消息2'},
                      9. {id: '03', title: '消息3'},
                      10. ]
                      11. }
                      12. render() {
                      13. const {messageArr} = this.state
                      14. return (
                        • {
                        • messageArr.map((msgObj) => {
                        • return (
                        • {/*${msgObj.id}/${msgObj.title}`}>{msgObj.title}*/}
                        • {/*${msgObj.id}&title=${msgObj.title}`}>{msgObj.title}*/}
                        • pathname: '/home/message/detail',
                        • state: {id: msgObj.id, title: msgObj.title}
                        • }}>{msgObj.title}
                        • )
                        • })
                        • }

                        • {/**/}
                        • {/**/}
                        • );
                        • }
                        • }
                        • export default Message;

                        18.编程式路由导航 

                        Message.jsx

                        1. import React, {Component} from 'react';
                        2. import Detail from "./Detail/Detail";
                        3. import {Link, Route} from "react-router-dom";
                        4. class Message extends Component {
                        5. state = {
                        6. messageArr: [
                        7. {id: '01', title: '消息1'},
                        8. {id: '02', title: '消息2'},
                        9. {id: '03', title: '消息3'},
                        10. ]
                        11. }
                        12. render() {
                        13. const {messageArr} = this.state
                        14. return (
                          • {
                          • messageArr.map((msgObj) => {
                          • return (
                          • {/*${msgObj.id}/${msgObj.title}`}>{msgObj.title}*/}
                          • {/*${msgObj.id}&title=${msgObj.title}`}>{msgObj.title}*/}
                          • pathname: '/home/message/detail',
                          • state: {id: msgObj.id, title: msgObj.title}
                          • }}>{msgObj.title}
                          • )
                          • })
                          • }

                          • {/**/}
                          • {/**/}
                          • );
                          • }
                          • }
                          • export default Message;

                           Detail.jsx

                          1. import React, {Component} from 'react';
                          2. import qs from "qs"
                          3. const DetailData = [
                          4. {id: '01', content: '你好,中国'},
                          5. {id: '02', content: '你好,尚硅谷'},
                          6. {id: '03', content: '你好,未来的自己'},
                          7. ]
                          8. class Detail extends Component {
                          9. render() {
                          10. console.log(this.props)
                          11. // const {id, title} = this.props.match.params
                          12. // const {search} = this.props.location
                          13. // const {id, title} = qs.parse(search.slice(1))
                          14. const {id, title} = this.props.location.state || {}
                          15. const findResult = DetailData.find((detailObj) => {
                          16. return detailObj.id === id
                          17. }) || {}
                          18. return (
                            • ID:{id}
                            • TITLE:{title}
                            • CONTENT:{findResult.content}
                            • );
                            • }
                            • }
                            • export default Detail;

                             19.withRouter的作用

                            News.jsx

                            1. import React, {Component} from 'react';
                            2. class News extends Component {
                            3. componentDidMount() {
                            4. setTimeout(() => {
                            5. this.props.history.push('/home/message')
                            6. }, 2000)
                            7. }
                            8. render() {
                            9. return (
                              • news001
                              • news002
                              • news003
                              • );
                              • }
                              • }
                              • export default News;

                               

                              1. withRouter可以加工一般组件,让一般组件具备路由组件所特有的API
                              2. withRouter的返回值是一个新组件 

                              Header.jsx

                              1. import React, {Component} from 'react';
                              2. import {withRouter} from "react-router-dom";
                              3. class Header extends Component {
                              4. back = () => {
                              5. this.props.history.goBack()
                              6. }
                              7. forward = () => {
                              8. this.props.history.goForward()
                              9. }
                              10. go = () => {
                              11. this.props.history.go(2)
                              12. }
                              13. render() {
                              14. // console.log('Header组件收到的props是', this.props)
                              15. return (
                              16. "page-header">
                              17. React Router Demo

                              18. );
                              19. }
                              20. }
                              21. export default withRouter(Header);

                              20.BrowserRouter与HashRouter

                            10. 相关阅读:
                              07-服务管理-03-自建yum仓库(手把手教你搭建内网yum源)
                              27、git的安装和配置(自用简易版)
                              第142篇:原生js实现响应式原理
                              SpringBoot项目基础设施搭建
                              华为OD机试真题- 非严格递增连续数字序列-2023年OD统一考试(B卷)
                              jjwt使用说明-笔记
                              Maven无法拉取SNAPSHOT依赖的解决办法
                              【开源】JAVA+Vue.js实现创意工坊双创管理系统
                              RK平台使用MP4视频做开机动画以及卡顿问题
                              Android面试指南
                            11. 原文地址:https://blog.csdn.net/qq_56444564/article/details/133899357