简体   繁体   English

为什么我不能使用 react-router-dom 中的“路由”,即使它是版本 6? 它显示错误

[英]Why am I not able to use "Routes" from react-router-dom even though it is version 6? It shows error

I have been trying to us 'Routes' from react-router-dom like this import { BrowserRouter as Router, Route, Routes } from 'react-router-dom';我一直在尝试从import { BrowserRouter as Router, Route, Routes } from 'react-router-dom'; -router-dom 中获取“路由”,就像这样And I call it here:我在这里称之为:

const NavPage = () => {
  return (
    <React.Fragment>
      <section>
        <Routes>
        <Route path="/home" element={<Home />} />
        <Route path="/goals" element={<Goals />} />
        <Route path="/meetings" element={<Meetings />} />
        <Route path="/tasks" element={<Tasks />} />
        <Route path="/reviews" element={<Reviews />} />
        <Route path="/feedback" element={<Feedback />} />
        <Route path="/recognitions" element={<Recognitions />} />
        <Route path="/notes" element={<Notes />} />
        </Routes>
      </section>      
    </React.Fragment>
  );
};

And it throws an error: export 'Routes' (imported as 'Routes') was not found in 'react-router-dom' (possible exports: BrowserRouter, HashRouter, Link, MemoryRouter, NavLink, Prompt, Redirect, Route, Router, StaticRouter, Switch, generatePath, matchPath, useHistory, useLocation, useParams, useRouteMatch, withRouter)它抛出一个错误: export 'Routes' (imported as 'Routes') was not found in 'react-router-dom' (possible exports: BrowserRouter, HashRouter, Link, MemoryRouter, NavLink, Prompt, Redirect, Route, Router, StaticRouter, Switch, generatePath, matchPath, useHistory, useLocation, useParams, useRouteMatch, withRouter)

My react-router-dom version is 6 enter image description here我的 react-router-dom 版本是 6 enter image description here

I tried using Switch but it is not working for me我尝试使用Switch ,但它对我不起作用

According to the image you have react-router-dom@^5.1.2 installed.根据图像,您安装了react-router-dom@^5.1.2

在此处输入图像描述

The latest version is v6.8.0.最新版本是 v6.8.0。 It looks like your IDE is just informing you what the latest version is when you hover over that dependency.看起来您的 IDE 只是在通知您 hover 超过该依赖项时最新版本是什么。

If you want to use the v6.8.0 then you'll need to actually install it and update your project's package.json file.如果您想使用 v6.8.0,那么您需要实际安装它并更新您项目的 package.json 文件。

From the terminal in the project's root directly run the following command to update to v.6.8.0 and update the package.json file.从项目根目录中的终端直接运行以下命令以更新到 v.6.8.0 并更新 package.json 文件。

npm install --save react-router-dom@latest

From here the project should have all the RRDv6 components available to it.从这里开始,项目应该拥有所有可用的 RRDv6 组件。

react-router-dom re-exports all of react-router so there's no need to have is listed additionally as a dependency, especially if it is still the previous version. react-router-dom重新导出react-router的所有内容,因此没有必要将其额外列为依赖项,特别是如果它仍然是以前的版本。 If react-router is installed, uninstall it, then install react-router-dom@6 .如果安装了react-router ,卸载它,然后安装react-router-dom@6

npm uninstall --save react-router

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM