简体   繁体   English

./src/App.js 尝试导入错误:“Switch”未从“react-router-dom”导出

[英]./src/App.js Attempted import error: 'Switch' is not exported from 'react-router-dom'

I can't figure it out why doesn't work.我不明白为什么不起作用。 I have uninstalled the react-router-dom package and reinstalled it, but I always have the same error.我已经卸载react-router-dom package 并重新安装它,但我总是有同样的错误。

The error I'm getting: ./src/App.js Attempted import error: 'Switch' is not exported from 'react-router-dom'.我得到的错误:./src/ App.js 尝试导入错误:'Switch' 不是从 'react-router-dom' 导出的。

This is my code.这是我的代码。 I hope that someone could give me a hand, Thanks in advance我希望有人能帮助我,在此先感谢

import './App.css';
import { BrowserRouter as Router, Switch, Route } from "react-router-dom";
import NavBar from '../src/Components/UI/NavBar/NavBar';
import Footer from '../src/Components/UI/Footer/Footer';
import Home from './Components/Views/Home/Home';

function App() {
  return (
    <Router>
      <NavBar/>
      <Switch>
        <Route path="/">
          <Home/>
        </Route> 
      </Switch>
      <Footer/>
    </Router>
  );
}

export default App;

If you accidentally updated react-router-dom to version 6 it no longer exports a Switch component.如果您不小心将react-router-dom更新到版本 6,它将不再导出Switch组件。 It was replaced by a Routes component that must directly wrap/render the Route components.它被一个必须直接包装/渲染Route组件的Routes组件所取代。

  1. Swap the Switch for the Routes component.交换Routes组件的Switch

     import { BrowserRouter as Router, Routes, Route } from "react-router-dom"; ... function App() { return ( <Router> <NavBar/> <Routes> <Route path="/" element={<Home/>} /> </Routes> <Footer/> </Router> ); }

    Follow the Upgrading from v5 to migrate your project from v5 to v6 across your app as there were quite a few breaking component API changes.遵循从 v5 升级以在您的应用程序中将项目从 v5 迁移到 v6,因为有很多破坏性组件 API 更改。

  2. Revert back to react-router-dom v5.恢复到react-router-dom v5。

    • Run npm un -s react-router-dom运行npm un -s react-router-dom
    • Run npm i -s react-router-dom@5.3.0运行npm i -s react-router-dom@5.3.0

I had the same problem and took me forever.我有同样的问题,并带我永远。 but mainly as you have asked recently the React-router-dom has been updated and they have removed Switch So try installing below code again.但主要是正如您最近所问的那样, React-router-dom 已经更新并且他们已经删除了Switch所以尝试再次安装下面的代码。 its version 5.它的第 5 版。

 npm install react-router-dom@5

I have also faced the same problem.我也遇到过同样的问题。 I had version 6 installed.我安装了版本 6。 Then I have found out that Switch was replaced by Routes .然后我发现SwitchRoutes取代了。 But changing to Routes was messing with my front-end.但是更改为路由会弄乱我的前端。 So I uninstall the version and went back to previous version by these commands:所以我卸载了这个版本并通过这些命令回到了以前的版本:

npm uninstall react-router-dom npm 卸载 react-router-dom

npm install react-router-dom@5.2.0 npm 安装 react-router-dom@5.2.0

This has solved my problem with 'Switch is not exported error'.这解决了我的“Switch is not exported error”问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误:./src/App.js 尝试导入错误:“Switch”未从“react-router-dom”导出 - Error: ./src/App.js Attempted import error: 'Switch' is not exported from 'react-router-dom' 尝试导入错误:“Switch”未从“react-router-dom”导出 - Attempted import error: 'Switch' is not exported from 'react-router-dom' 编译失败。/src/Content.js 尝试导入错误:“重定向”未从“react-router-dom”导出 - Failed to compile ./src/Content.js Attempted import error: 'Redirect' is not exported from 'react-router-dom' ./src/App.jsx 尝试导入错误:“路由”未从“react-router-dom”导出 - ./src/App.jsx Attempted import error: 'Routes' is not exported from 'react-router-dom' 尝试导入错误:“出口”未从“react-router-dom”导出 - Attempted import error: 'Outlet' is not exported from 'react-router-dom' 尝试导入错误:“useRouteMatch”未从“react-router-dom”导出 - Attempted import error: 'useRouteMatch' is not exported from 'react-router-dom' 尝试导入错误:“Outlet”未从“react-router-dom”导出 - Attempted import error: 'Outlet' is not exported from 'react-router-dom 尝试导入错误:“Navigate”未从“react-router-dom”导出 - Attempted import error: 'Navigate' is not exported from 'react-router-dom' 尝试导入错误:“useLocation”未从“react-router-dom”导出 - Attempted import error: 'useLocation' is not exported from 'react-router-dom' 尝试导入错误:“PrivateRoute”未从“react-router-dom”导出 - Attempted import error: 'PrivateRoute' is not exported from 'react-router-dom'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM