反应路由器不能以非常基本的方式工作

时间:2020-10-14 13:49:48

标签: reactjs react-router

嗨,他以非常基本的方式实施了react-router

App.js

import React from 'react';
import './App.css';
import { BrowserRouter as Router, Switch, Route, Link } from 'react-router-dom';

import About from './pages/About';
import Home from './pages/Home';

function App() {
  return (
    <Router>
      <div className='App'>
        <Link to='/about'>About</Link>
        <Link to='/home'>Home</Link>

        <Switch>
          <Route to='/about' exact component={About}></Route>
          <Route to='/home' exact component={Home}></Route>
        </Switch>
      </div>
    </Router>
  );
}

export default App;

index.js

ReactDOM.render(
  <React.StrictMode>
    <BrowserRouter>
      <App />
    </BrowserRouter>
  </React.StrictMode>,
  document.getElementById('root')
);

我想知道为什么这个基本设置不起作用?

我使用react-router-doc版本5.2.0

2 个答案:

答案 0 :(得分:2)

好像您在Route组件上传递了错误的道具。

尝试更改

<Route to='/about' exact component={About}></Route>

<Route path='/about' exact component={About}></Route>

答案 1 :(得分:0)

尝试一下

<Route exact path="/about" component={About} />

并更改此声明

import { BrowserRouter as Router, Switch, Route, Link } from 'react-router-dom';

import { Router, Switch, Route, Link } from 'react-router-dom';