即使存在,PhpStorm 也无法解析包导入(react-router-dom)

标签 phpstorm webstorm node-modules react-router-dom jetbrains-ide

我正在使用 React (16.12.0) 和 PhpStorm (2019.3.1)。

我要导入的包是 react-router-dom (5.1.2)

我将编写以下导入:

import { NavLink, Switch } from "react-router-dom";

这两个包都被 webpack/babel 正确导入,当我同时使用 Switch 和 NavLink 时页面正确呈现。

但纯粹从 IDE 的角度来看,我收到关于 Switch 的警告:Cannot resolve symbol 'Switch'

这很奇怪,因为它显然在那里,我检查了 /node_modules/react-router-dom 并且 Switch.js 在那里。

奇怪的是,在以前版本的 PhpStorm 上(在几次更新和插件导入和其他更改之前),PhpStorm 准确地找到了 Switch 导入。

我最近没有更新 react-router-dom 并且正在使用其最新的稳定版本。

关于为什么它可能缺少 Switch 的任何想法?

编辑: 我意识到这可能是因为 PhpStorm 在导入 commonJS 模块时遇到了困难。

我尝试将 Javascript 编译方法从 React JSX 更改为 ECMA 6。但没有成功。

我还尝试为 react-router-dom 导入一些构建库,这可以奇怪地识别 Route 导入,但不能识别 Switch

我也尝试过使 PhpStorm 缓存无效并重新启动应用程序,但这也没有用。

编辑 2: 根据答案,我尝试在光标位于 react-router-dom 内时使用 option+enter(Windows 上的 alt+enter)来尝试安装 typescript 包,但我没有得到JetBrains 网站上显示的相同建议操作:https://www.jetbrains.com/help/webstorm/configuring-javascript-libraries.html

它所建议的只是将单引号转换为双引号。

enter image description here

我也已经安装了@types/react-router-dom。也许我还缺少另一个?

enter image description here

编辑 3: 正确的答案是安装 @types/react-router。出于某种原因, @types/react-router-dom 欺骗性地不是解决这个问题的方法。

最佳答案

node_modules/react-router-dom/esm/react-router-dom.js 导出 BrowserRouter, HashRouter, Link NavLink,但不导出 Switch,它没有在那里明确定义。 为了更好的完成/类型提示,您可以为包安装 Typescript stub :将光标放在导入语句中的 "react-router-dom" 上,点击 Alt+Enter,选择安装 TypeScript 定义以获得更好的类型信息::

enter image description here

https://www.jetbrains.com/help/webstorm/2019.3/configuring-javascript-libraries.html#ws_jsconfigure_libraries_ts_definition_files

关于即使存在,PhpStorm 也无法解析包导入(react-router-dom),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60015377/

相关文章:

linux - Ubuntu/Mint 上的 PhpStorm 更新

rest - 使用 PHPStorm 或 POSTman 的 XDebug 和 RESTful 服务器

windows - 如何解决yaml/swagger中的错误?

c++ - Node 扩展问题

javascript - 如何使用nodejs只读取excel中的A列值?

node.js - 语义发布未安装并询问我无法更新的特定 Node 版本

intellij-idea - 如何将工作区(选项卡组)添加到 PhpStorm?

PhpStorm:获取 "Methods with the same name as their class will not be constructors"的代码检查警告

Webstorm 上下文文件夹总是包含在 url 路径中?

javascript - 使用名称 "in"的 Socket.io 使 Webstorm IDE 引发错误