美文网首页React
react-router-dom 6.0版本设置react路由

react-router-dom 6.0版本设置react路由

作者: 郭先生_515 | 来源:发表于2021-11-17 10:27 被阅读0次

    6.0版本之前路由设置:

    // App.js
    import React, { Component } from 'react';
    
    // 引入路由
    import { BrowserRouter as Router, Route, Link } from 'react-router-dom';
    
    // 路由页面
    import Home from './components/Home';
    import Login from './components/Login';
    
    class App extends Component {
        constructor(props) {
            super(props);
            this.state = {  }
        }
        render() { 
            return (
                <Router>
                    <ul className="topBar">
                        <li><Link to="/">Home</Link></li>
                        <li><Link to="/login">Login</Link></li>
                    </ul>
                    <Route exact path="/" component={Home}/>
                    <Route path="/login" component={Login}/>
                </Router>
            );
        }
    }
    export default App;
    

    6.0网上新版路由配置:

    // App.js
    import React, { Component } from 'react';
    
    // 引入路由
    import { BrowserRouter as Router, Routes, Route, Link } from 'react-router-dom';
    
    // 路由页面
    import Home from './components/Home'
    import Login from './components/Login'
    
    class App extends Component {
      constructor(props) {
        super(props);
        this.state = {  }
      }
      render() { 
        return (
          <Router>
            <ul className="topBar">
              <li><Link to="/">Home</Link></li>
              <li><Link to="/login">Login</Link></li>
            </ul>
            <Routes>
              <Route exact path="/" element={<Home/>}/>
              <Route path="/login" element={<Login/>}/>
            </Routes>
          </Router>
        );
      }
    }
    
    export default App;
    

    相关文章

      网友评论

        本文标题:react-router-dom 6.0版本设置react路由

        本文链接:https://www.haomeiwen.com/subject/ynlltrtx.html