下载 react-router-dom

    1. yarn add react-router-dom @types/react-router-dom
    1. import React, {
    2. Component
    3. } from 'react'
    4. import {Link,Route} from 'react-router-dom'
    5. import Home from './components/Home'
    6. import About from './components/About'
    7. import "./index.css"
    8. class App extends Component {
    9. render() {
    10. return (
    11. <div>
    12. <div className="box">
    13. <div className="box_list">
    14. <div>
    15. <Link to="/home"> 主页</Link>
    16. </div>
    17. <div>
    18. <Link to="/about">关于</Link>
    19. </div>
    20. </div>
    21. <div className="box_content">
    22. <Route path="/home" component={Home}/>
    23. <Route path="/about" component={About}/>
    24. </div>
    25. </div>
    26. </div>
    27. )
    28. }
    29. }
    30. export default App
    1. index.js
    2. import React from 'react';
    3. import ReactDOM from 'react-dom';
    4. import { BrowserRouter } from 'react-router-dom';
    5. import App from './App';
    6. ReactDOM.render(
    7. // React.StrictMode检查app和app包裹的组件是否合理
    8. <BrowserRouter>
    9. <App />
    10. </BrowserRouter>,
    11. document.getElementById('root')
    12. );

    需要使用BrowserRouter对根组件进行包裹,用link标签进行导航,使用route标签进行占坑。
    image.png