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

当前回答

你必须先检查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/>} />

其他回答

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

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;

Switch是从react-router导出的,而不是从react-router-dom导出的(我认为Route也一样)。

你只需要做两件事:

将Switch替换为路由。 使用这个Route标签

<Route path="/" element={<Component />} />

而不是这个:

<Route>
  path="/"
  <Component />
</Route>

第一步:升级到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及其语法的最新版本,而不是倒退。

Switch组件没有从react-router-dom库中导出。要使用Switch组件,您需要从react-router库中导入它。它的代码看起来像这样:

import { Switch } from 'react-router';