export 'Switch' (imported as 'Switch') was not found in 'react-router-dom'
时间: 2023-10-20 12:28:48 浏览: 37
这个错误通常是因为你使用了错误的导入方式。在 React Router v6 中,Switch 被移动到了 react-router 包中,所以你需要从 react-router 中导入 Switch,而不是从 react-router-dom 中导入。可以尝试使用如下代码导入 Switch:
```javascript
import { Switch } from 'react-router';
```
如果你仍然想从 react-router-dom 中导入 Switch,你需要将 React Router 升级为 v6 版本。如果你使用的是旧版本的 React Router,那么你需要从 react-router-dom 中导入 Switch:
```javascript
import { Switch } from 'react-router-dom';
```
希望这可以帮助你解决问题。
相关问题
export 'Switch' (imported as 'Switch') was not found in 'react-router'
根据提供的引用内容,当在使用`react-router`库时,出现了`export 'Switch' (imported as 'Switch') was not found in 'react-router'`的错误提示时,可能是因为`Switch`组件没有正确导入。这个错误通常发生在使用`react-router-dom`库时,将`Switch`组件错误地导入为`Switch`而不是`BrowserRouter`。
以下是一个示例,展示了如何正确导入和使用`Switch`组件:
```javascript
import { BrowserRouter, Switch, Route } from 'react-router-dom';
function App() {
return (
<BrowserRouter>
<Switch>
<Route exact path="/" component={Home} />
<Route path="/about" component={About} />
<Route path="/contact" component={Contact} />
</Switch>
</BrowserRouter>
);
}
```
请注意,正确导入`Switch`组件需要使用`react-router-dom`库,而不是`react-router`库。确保在项目中正确安装了`react-router-dom`库,并使用正确的导入语句。
export 'Routes' (imported as 'Routes') was not found in 'react-router-dom'
这错误是因为在使用react-router-dom时,导入的`Routes`并未在react-router-dom中找到。根据引用中的信息,可以尝试更改react-router-dom的版本号为5.x.x,并重新安装依赖项。但是根据引用中的提示,还需要手动卸载并重新安装react-router。具体做法是通过以下命令来卸载和重新安装react-router-dom和react-router:
```
npm uninstall react-router-dom react-router
npm install react-router-dom@5 react-router@5
```
这将卸载当前安装的react-router-dom和react-router,并重新安装5.x.x版本的react-router-dom和react-router,以解决`export 'Routes' (imported as 'Routes') was not found in 'react-router-dom'`的问题。<span class="em">1</span><span class="em">2</span><span class="em">3</span><span class="em">4</span>