1.模糊匹配

  1. 一般条件下,路由是模糊匹配的// import axios from 'axios'
  2. import React, {
  3. Component
  4. } from 'react'
  5. import {Link,Route,Switch,Redirect} from 'react-router-dom'
  6. import Home from './components/Home'
  7. import About from './components/About'
  8. import Login from './components/Login'
  9. // import AddName from './components/Text'
  10. import "./index.css"
  11. class App extends Component {
  12. render() {
  13. return (
  14. <div>
  15. <div className="box">
  16. <div className="box_list">
  17. <div>
  18. <Link to="/home/a/b"> 主页</Link>
  19. </div>
  20. <div>
  21. <Link to="/about">关于</Link>
  22. </div>
  23. </div>
  24. <div className="box_content">
  25. <Switch>
  26. <Route path="/home" component={Home}/>
  27. <Route path="/about" component={About}/>
  28. <Route path="/about" component={Login}/>
  29. </Switch>
  30. </div>
  31. </div>
  32. </div>
  33. )
  34. }
  35. }
  36. export default App
  37. // import axios from 'axios'
  38. import React, {
  39. Component
  40. } from 'react'
  41. import {Link,Route,Switch,Redirect} from 'react-router-dom'
  42. import Home from './components/Home'
  43. import About from './components/About'
  44. import Login from './components/Login'
  45. // import AddName from './components/Text'
  46. import "./index.css"
  47. class App extends Component {
  48. render() {
  49. return (
  50. <div>
  51. <div className="box">
  52. <div className="box_list">
  53. <div>
  54. <Link to="/home"> 主页</Link>
  55. </div>
  56. <div>
  57. <Link to="/about">关于</Link>
  58. </div>
  59. </div>
  60. <div className="box_content">
  61. <Switch>
  62. <Route path="/home" component={Home}/>
  63. <Route path="/about" component={About}/>
  64. <Route path="/about" component={Login}/>
  65. <Redirect to="/home"></Redirect>
  66. </Switch>
  67. </div>
  68. </div>
  69. </div>
  70. )
  71. }
  72. }
  73. export default App

image.png

2.精准匹配

精准匹配就是在route组件上加上一个exact属性
image.png

  1. // import axios from 'axios'
  2. import React, {
  3. Component
  4. } from 'react'
  5. import {Link,Route,Switch,Redirect} from 'react-router-dom'
  6. import Home from './components/Home'
  7. import About from './components/About'
  8. import Login from './components/Login'
  9. // import AddName from './components/Text'
  10. import "./index.css"
  11. class App extends Component {
  12. render() {
  13. return (
  14. <div>
  15. <div className="box">
  16. <div className="box_list">
  17. <div>
  18. <Link to="/home/a/b"> 主页</Link>
  19. </div>
  20. <div>
  21. <Link to="/about">关于</Link>
  22. </div>
  23. </div>
  24. <div className="box_content">
  25. <Switch>
  26. <Route path="/home" component={Home} exact/>
  27. <Route path="/about" component={About}/>
  28. {/* <Redirect to="/home"></Redirect> */}
  29. </Switch>
  30. </div>
  31. </div>
  32. </div>
  33. )
  34. }
  35. }
  36. export default App