简体   繁体   English

在 React 开发中 - VS Code:“转到定义”不适用于导出的类

[英]In React Development - VS Code: “go to definition” not working on exported class

I can't use "go to definition" feature in VS Code on exported class clicking on method, it just says "no definition found"我无法在 VS Code 中在导出类单击方法时使用“转到定义”功能,它只是说“未找到定义”

After googling found that the imported class must have typescript definition files to this feature to work.谷歌搜索后发现导入的类必须有打字稿定义文件才能使用此功能。 How to create these definition files and how to include in react app, I am using web pack (create-react-app tool).如何创建这些定义文件以及如何包含在 react 应用程序中,我使用的是 web pack(create-react-app 工具)。

There are a few things you can do. 您可以做几件事。

If the imported component is a library you can try and install @types/library or check whether the types are available for that library 如果导入的组件是库,则可以尝试安装@ types / library或检查类型是否适用于该库

If it was created by you or if you have the source-code of the class you can set declaration: true in tsconfig.json . 如果它是由您创建的,或者您具有该类的源代码,则可以在tsconfig.json设置declaration: true It will automatically generate the .d.ts file 它将自动生成.d.ts文件

Or you can create a .d.ts file. 或者,您可以创建一个.d.ts文件。 Its actually not that complicated. 它实际上并不那么复杂。 For more details on this check out https://www.typescriptlang.org/docs/handbook/declaration-files/introduction.html 有关此的更多详细信息,请查看https://www.typescriptlang.org/docs/handbook/declaration-files/introduction.html

I was facing the same issue and in my case, it worked doing the following.我遇到了同样的问题,就我而言,它可以执行以下操作。

Try creating a jsconfig.json at the root of the workspace with the content:尝试在工作区的根目录创建一个jsconfig.json ,内容如下:

{
    "compilerOptions": {
        "jsx": "react"
    }
}

This tells our JS language features to also handle JSX files in a project.这告诉我们的 JS 语言特性也可以处理项目中的 JSX 文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM