【问题标题】:What causes the typescript Module has no exported member .ts(2305) error and how do you fix it?是什么导致打字稿模块没有导出的成员 .ts(2305) 错误,你如何解决它?
【发布时间】:2021-12-07 06:33:44
【问题描述】:

这是我第一次在 React 中使用 typescript,我遇到了很多关于 typescript Module has no exported member .ts(2305) 错误的麻烦。以下是发生这种情况的 2 个示例:

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-domyarn add @types/react-router 最后是 yarn add @types/react-native-vector-icons 并尝试简单地 yarn install 但这从未解决问题。

【问题讨论】:

    标签: reactjs typescript


    【解决方案1】:

    如果您从 v5 升级并看到此问题,我删除了 @types/react-router-dom 依赖项并修复了此问题,因为 v6 的类型随 react-router-dom 一起提供。 @types 依赖项本身并未从 v5 中删除。

    【讨论】:

      【解决方案2】:

      这不是一般错误,而是非常具体的错误。此错误表明您正在尝试导入不存在的内容。

      每个问题的答案取决于您要导入的内容。

      • Routes 不是react-router-dom 的命名导出。无处in the documentation 是字符<Routes。我不知道你认为你在导入什么,但你可能想要别的东西。

      • MaterialCommunityIcons 不是react-native-vector-icons 的命名导出。根据the documentation,您需要像这样导入:

        import MaterialCommunityIcons from 'react-native-vector-icons/MaterialCommunityIcons';
        
      • useNavigate 不是react-router 的命名导出。 The documentation 列出 useNavigation。你的意思是导入那个吗?

      如您所见,没有一种万能的解决方案。您必须查阅文档并尝试找到要导入的内容并确保您得到正确的内容。

      【讨论】:

      • 啊,我没有意识到这一点。谢谢亚历克斯的解释!
      【解决方案3】:

      对于实例 react-router-dom 版本 5 来说,你的包 json 中的那个没有路由,版本 6 是有路由的那个。在这里你可以找到更多信息https://reactrouter.com/web/guides/quick-start

      【讨论】:

        猜你喜欢
        • 2021-12-25
        • 2021-01-29
        • 1970-01-01
        • 2020-02-08
        • 2017-06-11
        • 1970-01-01
        • 2017-06-13
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多