![](/img/trans.png)
[英]Attempted import error: 'Routes' is not exported from 'react-router-dom'
[英]Why am I not able to use "Routes" from react-router-dom even though it is version 6? It shows error
我一直在嘗試從import { BrowserRouter as Router, Route, Routes } from 'react-router-dom';
-router-dom 中獲取“路由”,就像這樣我在這里稱之為:
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>
);
};
它拋出一個錯誤: 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)
我的 react-router-dom 版本是 6 enter image description here
我嘗試使用Switch
,但它對我不起作用
根據圖像,您安裝了react-router-dom@^5.1.2
。
最新版本是 v6.8.0。 看起來您的 IDE 只是在通知您 hover 超過該依賴項時最新版本是什么。
如果您想使用 v6.8.0,那么您需要實際安裝它並更新您項目的 package.json 文件。
從項目根目錄中的終端直接運行以下命令以更新到 v.6.8.0 並更新 package.json 文件。
npm install --save react-router-dom@latest
從這里開始,項目應該擁有所有可用的 RRDv6 組件。
react-router-dom
重新導出react-router
的所有內容,因此沒有必要將其額外列為依賴項,特別是如果它仍然是以前的版本。 如果安裝了react-router
,卸載它,然后安裝react-router-dom@6
。
npm uninstall --save react-router
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.