我不知道为什么我得到这个错误,我在任何地方都找不到答案。我已经卸载了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

其他回答

我解决了我的错误,通过改变我渲染我的路线使用元素的方式。

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;

一个解决方案:

删除“node_modules”文件夹。在包装里。Json文件,更改react-router-dom版本(在我的情况下版本6)为“react-router-dom”:“^5.2.1”

然后在终端运行:

NPM install用于安装依赖项,然后运行 NPM开始重新启动

在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>
        );
    }
}

我可以通过将Switch更改为Routes来修复它。 所以你应该有这样的东西:

<Routes>
  <Route path='/home' element={<Home/>} />
</Routes>

你还需要把import从Switch改成Routes:

import { Routes, Route } from "react-router-dom";

在react-router-dom v6中,Switch被替换为路由。

还有组件和元素

{componentName}和{}