javascript 我有这个错误:“在'react-router-dom'中未找到导出'Switch'(导入为'Switch')“

hts6caw3  于 2023-04-19  发布在  Java
关注(0)|答案(1)|浏览(220)

我有react-dom v^18.2.0

我的代码:
从“./Home”导入Home;从“./Navbar”导入Navebar;
import { BrowserRouter as Router,Route,Switch } from 'react-router-dom';

function App() {

  
  return (
    <Router>
      <div className="App">
        <Navebar/>
        
        <div className="content">
          <Switch>
            <Route path="/">
              <Home />
            </Route>
          </Switch>
        </div>
        
      </div>
    </Router>
  );
}

export default App;
carvr3hs

carvr3hs1#

如果您使用react-router-dom v6或更高版本代替Switch,请使用Routes:-

import Home from "./Home"; import Navebar from "./Navbar";
import { BrowserRouter as Router, Route, Routes} from "react-router-dom";

function App() {

  return (
<Router>
  <div className="App">
    <Navebar />
    <div className="content">
      <Routes>
        <Route path="/">
          <Home />
        </Route>
      </Routes>
    </div>
  </div>
</Router>
  );
}

export default App;

请按照react-router-dom更新文档从v5到v6:-
文档链接

相关问题