我不知道为什么我得到这个错误,我在任何地方都找不到答案。我已经卸载了react-router-dom包并重新安装了它,但它仍然告诉我交换模块没有从react-router-dom导出。这是我的代码。
我得到的错误是:
试图导入错误:“Switch”没有从“react-router-dom”中导出。
Code
import React from 'react';
import './App.css';
import NavBar from './components/navbar.js';
import Footer from './components/footer.js';
import Home from './components/pages/homepage/home.js';
import { BrowserRouter as Router, Switch, Route, Link } from 'react-router-dom';
function App() {
return (
<Router>
<div className="app-container">
<NavBar />
<Switch>
<Route path="/home" component={Home} />
</Switch>
<Footer />
</div>
</Router>
);
}
export default App;
在react-router-dom v6中,“Switch”被替换为路由“routes”。您需要更新从的导入
import { Switch, Route } from "react-router-dom";
to
import { Routes ,Route } from 'react-router-dom';
您还需要更新Route声明
<Route path="/" component={Home} />
to
<Route path='/' element={<Home/>} />
在react-router-dom中,你也不需要在Route声明中使用exact。
欲了解更多信息,请访问官方文档:
升级到react-router-dom v6
改变
import { BrowserRouter as Router, Switch, Route, Link } from "react-router-dom";
<Switch>
<Route path="/home" component={Home} />
</Switch>
to
import { BrowserRouter as Router, Routes, Route, Link } from "react-router-dom";
<Routes>
<Route path="/home" element={ <Home />} />
</Routes>
如果你正在使用react-router-dom v6,你必须改变Switch到Routes,如下面的例子所示:
而不是导入Switch,
import {Switch,…} from 'react-router-dom';
进口航线
import{路由,…} from 'react-router-dom';
然后,不要使用component ={ComponentName},而是使用element={<ComponentName/>},如下所示:
import { Routes, Route, ...} from 'react-router-dom';
.
.
.
<Routes>
<Route exact path='/' element={<Home/>}></Route>
</Routes>
我解决了我的错误,通过改变我渲染我的路线使用元素的方式。
To:
import React from "react";
import { BrowserRouter as Router, Route, Routes } from "react-router-dom";
import { Home } from "./app";
import { Login, Register } from "./auth";
const R: React.FC = () => {
return (
<Router>
<Routes>
<Route path="/login" caseSensitive={false} element={<Login />} />
<Route path="/register" caseSensitive={false} element={<Register />} />
<Route path="/" caseSensitive={false} element={<Home />} />
</Routes>
</Router>
);
};
export default R;
基本上在v6之前。*:
import React from "react";
import { BrowserRouter as Router, Route, Switch} from "react-router-dom";
import { Home } from "./app";
import { Login, Register } from "./auth";
const R: React.FC = () => {
return (
<Router>
<Switch>
<Route path="/login">
<Login />
</Route>
<Route path="/register">
<Register/>
</Route>
<Route path="/">
<Home/>
</Route>
</Switch>
</Router>
);
};
export default R;
在v6 . *
import React from "react";
import { BrowserRouter as Router, Route, Routes } from "react-router-dom";
import { Home } from "./app";
import { Login, Register } from "./auth";
const R: React.FC = () => {
return (
<Router>
<Routes>
<Route path="/login" caseSensitive={false} element={<Login />} />
<Route path="/register" caseSensitive={false} element={<Register />} />
<Route path="/" caseSensitive={false} element={<Home />} />
</Routes>
</Router>
);
};
export default R;
语法已经改变
旧的语法
import { BrowserRouter as Router, Switch, Route, Link } from "react-router-dom";
<Switch>
<Route path="/home" component={Home} />
</Switch>
新语法:
import { BrowserRouter as Router, Routes, Route, Link } from "react-router-dom";
<Routes>
<Route path="/home" element={<Home/>} />
</Routes>
我和杂志海报上的人面临着同样的问题。我尝试了以下所有的方法:
卸载并安装react-router-dom
分别从react-router-dom导入Switch
但对我来说什么都没用。
请遵循以下指示。我相信你不会再得到错误。
正确的代码:
import React from "react";
import "./App.css";
import NavBar from "./components/navbar.js";
import Footer from "./components/footer.js";
import Home from "./components/pages/homepage/home.js";
import { BrowserRouter as Router, Routes, Route, Link } from "react-router-dom";
function App() {
return (
<Router>
<div className="app-container">
<NavBar />
<Routes>
<Route path="/home" element={<Home/>} />
</Routes>
<Footer />
</div>
</Router>
);
}
export default App;
注意:Switch已经被替换为Routes,我们需要使用element而不是component。
你必须先检查npm包的版本。
执行npm info react-router-dom version命令查看。
如果包版本为>= 6.0.0,则必须进行更改
import { BrowserRouter as Router, Switch, Route, Link } from "react-router-dom";
to
import { BrowserRouter as Router,Routes, Route, Link } from "react-router-dom";
也必须改变
<Route path="/home" component={Home} />
to
<Route path="/home" element={<Home/>} />
我也面临着这个问题,并最终通过安排代码解决了它。我是React的新手。
以下是我的App.js代码(类基组件):
import logo from './logo.svg';
import './App.css';
import React, { Component } from 'react'
import PropTypes from 'prop-types'
import Navbar from './components/Navbar';
import News from './components/News';
import { BrowserRouter as Router, Route, Routes,Link } from 'react-router-dom'
export class App extends Component {
static propTypes = {
}
render() {
return (
<>
<Navbar/>
<Router>
<Routes>
<Route path="/general"><News pageSize={3} country={'us'} category={'general'}/></Route>
</Routes>
</Router>
</>
)
}
}
export default App
如果你正在使用react-router-dom的版本6,请使用这个
另外,请参考这里:https://reactrouter.com/docs/en/v6/upgrading/v5#:~:text=single%20route%20config%3A-,//%20This%20is%20a%20React%20Router%20v6%20app,%7D,-This%20step%20is
// This is a React Router v6 app
import {
BrowserRouter,
Routes,
Route,
Link,
Outlet
} from "react-router-dom";
function App() {
return (
<BrowserRouter>
<Routes>
<Route path="/" element={<Home />} />
<Route path="users" element={<Users />}>
<Route path="me" element={<OwnUserProfile />} />
<Route path=":id" element={<UserProfile />} />
</Route>
</Routes>
</BrowserRouter>
);
}
function Users() {
return (
<div>
<nav>
<Link to="me">My Profile</Link>
</nav>
<Outlet />
</div>
);
}
第一步:升级到React 16.8+和React -router-dom v5.2:
npm uninstall react-router-dom
npm install react-router-dom@5.2
步骤2:更新react-router-dom导入语句。
修改import {Switch, Route} from "react-router-dom";:
import { Routes, Route } from 'react-router-dom';
第三步:升级语法,把“Switch”换成“Routes”,把“component”换成“element”
改变
<Switch>
<Route path="/home" component={HomePage} />
</Switch>
to
<Routes>
<Route path="/home" element={<HomePage/>} />
</Routes>
或者,您也可以按照其他解决方案中的建议降低react-router版本。
但是,我建议升级到react-router-dom及其语法的最新版本,而不是倒退。
在react-router-dom v6中,开关被路由替换。下面是配置react-router-dom v6的简单示例。
import * as React from 'react';
import { BrowserRouter as Router, Route, Switch } from 'react-router-dom';
import Layout from './components/Layout';
import ProtectedRoute from './components/ProtectedRoute';
import LoginPage from './pages/LoginPage';
import WizardPage from './pages/WizardPage';
import RequestsPage from './pages/RequestsPage';
import './App.scss'
export class App extends React.Component {
public render() {
const sharedRouteProps = { exact: true, authenticationPath: '/login' };
const wizardRouteProps = { ...sharedRouteProps, path: "/", component: WizardPage };
const requestsRouteProps = { ...sharedRouteProps, path: "/requests", component: RequestsPage };
return (
<Layout>
<Router>
<Switch>
<Route exact path='/login' component={LoginPage} />
<ProtectedRoute {...wizardRouteProps} />
<ProtectedRoute {...requestsRouteProps} />
</Switch>
</Router>
</Layout>
);
}
}
export 'Switch'(作为'Switch'导入)在'react-router-dom'中找不到
第一种方法[用路由替换交换机]
import { Routes , Route , Link } from 'react-router-dom';
<Routes>
<Route exact path='/'>
<HomePage />
</Route>
</Routes>
第二种方法[将react-router-dom版本降级至版本5或以下]
npm i react-router-dom@5.0.0