export 'Switch' (imported as 'Switch') was not found in 'react-router-dom'
时间: 2023-10-20 22:28:48 浏览: 159
这个错误通常是因为你使用了错误的导入方式。在 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 'withRouter' (imported as 'withRouter') was not found in 'react-router-dom'
The error message you encountered suggests that the named export 'withRouter' was not found in the 'react-router-dom' package. This error commonly occurs when using the latest versions of React Router, as the 'withRouter' higher-order component has been deprecated.
Instead of using 'withRouter', you can access the router props by using the 'useHistory', 'useLocation', or 'useParams' hooks provided by React Router. These hooks allow you to access the history, location, and params objects directly within your functional components.
For example, to access the history object, you can use the 'useHistory' hook:
```jsx
import { useHistory } from 'react-router-dom';
function MyComponent() {
const history = useHistory();
// Access history methods (e.g., push, replace, goBack) here
// ...
}
```
Similarly, you can use the 'useLocation' and 'useParams' hooks to access the location and params objects, respectively.
Please note that these hooks are only available in functional components. If you are using a class component, you might need to consider using a different approach or wrapping your class component with a functional component that utilizes these hooks.
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>
阅读全文