reactjs - 无法解析符号 useParams

标签 reactjs react-router react-hooks webstorm react-router-dom

我正在创建一个使用 WebStorm 使用react的应用程序。我正在尝试使用 react-router-dom useParams。我有 react-router-dom 5.1.2 作为依赖项,但 useParams 在 import 语句中是灰色的。当我将鼠标悬停在 useParams 上时,它会显示“无法解析符号 useParams”。我也在尝试导入 Switch,但我收到了类似的消息“无法解析符号 Switch”。更不寻常的是 Link 是从 react-router-dom 导入的。

enter image description here enter image description here enter image description here

最佳答案

node_modules/react-router-dom/esm/react-router-dom.js转口Switch , Root , useParams来自 react-router .但后者并未在您的 package.json 中列为直接依赖项。因此被排除在索引之外。
您可以通过选择 取消排除它将目录标记为/不排除 来自 node_modules/react-router右键菜单:

enter image description here

但我强烈建议为包安装 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

关于reactjs - 无法解析符号 useParams,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60424238/

相关文章:

javascript - 使用键映射和过滤对象数组

reactjs - react : on page load Fade image in

CSS 在均匀分布的网格中布置元素列表

reactjs - Redux router - 刷新后如何重放状态?

reactjs - Jest : setTimeout is being called too many times

reactjs - 试图准确理解当我使用 useEffect 时发生了什么

javascript - useState 不适用于数组,React Hooks

javascript - NextJS & json-server 使用 getStaticPaths 在 localhost 上加载缓慢

javascript - 最佳实践 : Handle read-only data with Redux

reactjs - 使用 React Router 重定向页面的最佳方法是什么?