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

当前回答

Switch已被Routes替换。

更新路由(react-router-dom语法)#1386 (howtographql GitHub)

其他回答

我也面临着这个问题,并最终通过安排代码解决了它。我是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
import React from "react";
import { Link, BrowserRouter as Router, Route, Routes } from "react-router-dom";
import "./App.css";
import App from "./App";
const RouterPage = () => {
    return (
        <Router>
            <div>
                <nav>
                    <Link to="/">Home</Link>
                    <Link to="/posts">Posts</Link>
                    <Link to="/">About</Link>
                    <Link to="/contact">Contact</Link>
                </nav>
                <Routes>
                    <Route exact path="/" element={<App />}></Route>
                    <Route path="/post" element={<App />} />
                    <Route path="/about" element={<App />} />
                    <Route path="/contact" element={<App />} />
                </Routes>
            </div>
        </Router>
    );
};

你应该把Switch换成Routes

第一步:升级到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中,Switch被替换为Routes。 使用以下格式:

import { BrowserRouter as Router, Routes, Route} from 'react-router-dom';

<Router>
    <Routes>
        <Route exact path="/" component={component} />
        <Route exact path="/path2" component={component2} />
        <Route exact path="/path3" component={component3} />
    </Routes>
</Router>

我也有同样的问题。在项目终端上,输入以下命令。您不需要对代码进行任何更改。

卸载NPM react-router-dom NPM安装react-router-dom@5.2.0