这是我第一次在React中使用typescript,我在typescript Module has no exported member .ts(2305)
错误中遇到了很多麻烦。以下是发生这种情况的两个示例:
import { BrowserRouter, Routes, Route } from 'react-router-dom';
错误:
'"react-router-dom"' has no exported member named 'Routes'. Did you mean 'Route'?ts(2724)
还有这里
import { useNavigate } from 'react-router';
错误:
Module '"react-router"' has no exported member 'useNavigate'.ts(2305)
最后一个例子:
import { MaterialCommunityIcons } from 'react-native-vector-icons';
错误:
Module '"react-native-vector-icons"' has no exported member 'MaterialCommunityIcons'.ts(2305)
下面是我的package.json文件,如果有帮助的话:
{
"name": "frontend",
"version": "0.1.0",
"private": true,
"dependencies": {
"@emotion/react": "^11.4.1",
"@emotion/styled": "^11.3.0",
"@testing-library/jest-dom": "^5.11.4",
"@testing-library/react": "^11.1.0",
"@testing-library/user-event": "^12.1.10",
"@types/history": "2.0.45",
"@types/jest": "^26.0.15",
"@types/node": "^12.0.0",
"@types/react": "^17.0.0",
"@types/react-dom": "^17.0.0",
"@types/react-router": "3",
"@types/react-router-dom": "^5.1.8",
"community": "^0.0.1",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-prettier": "^3.4.0",
"history": "^5.0.1",
"icons": "^1.0.0",
"material": "^0.4.3",
"prettier": "^2.3.2",
"react": "^17.0.1",
"react-bootstrap": "^1.6.1",
"react-dom": "^17.0.1",
"react-native-vector-icons": "^8.1.0",
"react-router-dom": "^5.2.0",
"react-scripts": "4.0.1",
"react-vector-icons": "^0.0.1",
"typescript": "^4.1.2",
"web-vitals": "^1.0.1"
},
出现此问题时,我可以做些什么来解决它?我问的不是这3个特别的问题,而是如何解决Module has no exported member .ts(2305)
错误,因为它似乎经常出现。
我已经读了几次,并试图安装类型定义(我想?)如下所示:
yarn add @types/react-router-dom
和yarn add @types/react-router
,最后是yarn add @types/react-native-vector-icons
,并尝试简单地使用yarn install
,但这从未解决过问题。
发布于 2021-08-23 22:25:35
这不是一个一般性的错误,它是一个非常特殊的错误。此错误告诉您,您正在尝试导入不存在的内容。
每种方法的答案都取决于您要导入的内容。
Routes
不是react-router-dom
的命名导出。无处不在是the characters <Routes
。我不知道你认为你在导入什么,但你可能想要别的东西。--MaterialCommunityIcons
不是react-native-vector-icons
的命名导出。根据the documentation的说法,你需要像这样导入:从'react-native-vector-icons/MaterialCommunityIcons';导入MaterialCommunityIcons
useNavigate
不是react-router
的命名导出。The documentation列出了useNavigation
。您是否打算改为导入它?正如您所看到的,没有万能的解决方案。您必须查阅文档,尝试找到要导入的内容,并确保获得正确的内容。
发布于 2021-12-06 22:33:44
如果你从v5升级并看到这个问题,我删除了@types/react-router-dom
依赖,它修复了这个问题,因为v6的类型是随react-router-dom
一起提供的。v5本身并不会影响@types
依赖项。
发布于 2021-08-23 22:18:43
好吧,对于instans react-router-dom版本5,你的包json中的那个没有路由,版本6是有路由的。在这里您可以找到更多信息https://reactrouter.com/web/guides/quick-start
https://stackoverflow.com/questions/68899565
复制相似问题