一、安装依赖

  1. npm install react-router-dom

二、配置路由

在App.js

  1. import {
  2. BrowserRouter as Router,
  3. Switch,
  4. Route,
  5. Link
  6. } from "react-router-dom";

Tip: exact 路由严格匹配 只有路径为/才会加载对应路由 Switch 只匹配对应路径的路由

  1. import Home from './views/Home'
  2. import About from './views/About'
  3. class App extends Component {
  4. render() {
  5. return (
  6. <div>
  7. <Router>
  8. <ul>
  9. <li><Link to="/">首页</Link></li>
  10. <li><Link to="/about">about</Link></li>
  11. </ul>
  12. <Switch>
  13. <Route exact path="/" component={Home}></Route>
  14. <Route path="/about" component={About}></Route>
  15. <Route path="/about/la" component={About}></Route>
  16. </Switch>
  17. </Router>
  18. </div>
  19. );
  20. }
  21. }

三、get传值

3-1 路由跳转

  1. import {Link} from 'react-router-dom';
  2. <div>
  3. 主页
  4. <Link to={`/detail?id=${this.state.id}`}>
  5. <Button>detail</Button>
  6. </Link>
  7. </div>

3-2 事件跳转

  1. <div>
  2. 主页
  3. <Button onClick={this.handleToggle}>detail</Button>
  4. </div>
  5. handleToggle=()=>{
  6. this.props.history.push(`/detail?id=${this.state.id}`)
  7. }

3-3 query-string解析get传值

  1. //安装依赖
  2. yarn add query-string
  1. //Detail.js
  2. //this.props.location.search
  3. import queryString from 'query-string'
  4. class Detail extends Component {
  5. ...
  6. componentDidMount() {
  7. var url = this.props.location.search;
  8. console.log(queryString.parse(url))
  9. }
  10. }
  11. export default Detail;

四、动态路由

4-1 配置动态路由

  1. <Route path="/detail/:id" component={Detail}></Route>

4-2 跳转

  1. <Button onClick={this.handleToggle}>detail</Button>
  2. handleToggle=()=>{
  3. this.props.history.push(`/detail/${this.state.id}`)
  4. }

4-3 详情页接收

  1. // this.props.match.params
  2. componentDidMount() {
  3. console.log(this.props.match.params)
  4. }

五、嵌套路由,路由重定向

5-1 在list路由下挂载二级路由

  1. class List extends Component {
  2. render() {
  3. return (
  4. <div>
  5. <ul>
  6. <li>
  7. <Link to="/list/morning">新闻详情</Link>
  8. </li>
  9. <li>
  10. <Link to="/list/night">晚间新闻</Link>
  11. </li>
  12. </ul>
  13. <div>
  14. <Switch>
  15. <Route path="/list/morning" component={Morning}></Route>
  16. <Route path="/list/night" component={Night}></Route>
  17. //路由重定向
  18. <Redirect from="/list" to="/list/morning"></Redirect>
  19. </Switch>
  20. </div>
  21. </div>
  22. );
  23. }
  24. }
  25. export default List;

5-2 this.props.match.url

  1. //二级路由也可以配置成这样
  2. <Route path={`${this.props.match.url}/list/morning`} component={Morning}></Route>

六、路由模块化

  1. import React, { Component } from 'react';
  2. import {
  3. Switch,
  4. Route,
  5. Link,
  6. Redirect
  7. } from "react-router-dom";
  8. import Morning from './Morning'
  9. import Night from './Night'
  10. let routes = [
  11. {path:'/morning',component:Morning},
  12. {path:'/night',component:Night}
  13. ]
  14. class List extends Component {
  15. render() {
  16. return (
  17. <div>
  18. <ul>
  19. <li>
  20. <Link to="/list/morning">早间新闻</Link>
  21. </li>
  22. <li>
  23. <Link to="/list/night">晚间新闻</Link>
  24. </li>
  25. </ul>
  26. <div>
  27. <Switch>
  28. {routes.map(item=>{
  29. return (<Route key={item.path} path={this.props.match.url+item.path} component={item.component}></Route>)
  30. })}
  31. <Redirect from="/list" to="/list/mornig"></Redirect>
  32. </Switch>
  33. </div>
  34. </div>
  35. );
  36. }
  37. componentDidMount(){
  38. console.log(this.props.match.url)
  39. }
  40. }
  41. export default List;

七、二级路由

  1. import React, { Component } from 'react';
  2. import {
  3. Switch,
  4. Route,
  5. Redirect
  6. } from "react-router-dom";
  7. import Morning from './components/Morning'
  8. import Night from './components/Night'
  9. class About extends Component {
  10. render() {
  11. return (
  12. <div>
  13. 关于页面
  14. <div>
  15. <Switch>
  16. <Route path="/about/morning">
  17. <Morning/>
  18. </Route>
  19. <Route path="/about/night">
  20. <Night/>
  21. </Route>
  22. //路由重定向
  23. <Redirect from="/about" to="/about/morning"></Redirect>
  24. </Switch>
  25. </div>
  26. </div>
  27. );
  28. }
  29. }
  30. export default About;
  1. //使用component的方法的话,route标签不能有空格
  2. <Route exact path="/" component={Home}></Route>