英文:
VS Code Go to Implementations / Definition (aka F12, aka CTRL+click) doesn't work
问题
通常,当我在JavaScript文件中的函数定义上选择“转到定义”或“转到实现”选项,或者按F12或CTRL + 单击函数名称,VS Code会在弹出窗口中显示在整个项目中使用该函数的所有位置。
但出于某种原因,这对我的一些项目不起作用。
以下是一个简单的重现结构,对我来说仍然存在问题:
foo.js
bar
|_ bar.js
// foo.js
import bar from './bar/bar';
export const foo = () => {
bar();
};
// bar/bar.js
export default function bar() {
console.log('调用函数bar')
}
就是这样。
当编辑器中同时打开这两个文件时,该命令可以正常工作。如果这两个文件位于同一目录中,无论是根目录还是更深的目录,该命令也可以正常工作。如果我重命名或移动其中一个文件,更改会正确自动反映在另一个文件的导入行中,这意味着VS Code理解文件结构。
为什么既不出现定义/实现弹出窗口,也不出现.js文件,也不出现.ts文件?
英文:
Normally when I choose either "Go to Definition" or "Go to Implementations" from the context menu on a function name of a function definition in a JavaScript file, or press F12
or CTRL+Click
on the function name, VS Code shows in a popup all the places across the project where that function is used.
For some reason this doesn't work with some of my projects.
Here is a simple repro structure that still has the problem for me:
foo.js
bar
|_ bar.js
// foo.js
import bar from './bar/bar';
export const foo = () => {
bar();
};
// bar/bar.js
export default function bar() {
console.log('function bar called')
}
That's all.
The command works fine when both files are open in the editor. The command also works if both files are in the same directory, no matter root or deeper. If I rename or move one of the files, the changes get correctly automatically reflected in the import line of the other file, which means VS Code understands the file structure.
Why doesn't the definitions / implementations popup appear, neither for .js
files, nor for .ts
files?
答案1
得分: 0
你必须在项目的根目录中包含一个 jsconfig.json
或 tsconfig.json
文件,以便 IntelliSense 理解你确实处于一个 JavaScript 项目中,并将文件链接在一起,而不是将它们视为独立实体。请参阅 jsconfig.json 的详细信息 在 VS Code 文档中。
英文:
You must include a jsconfig.json
or a tsconfig.json
file in the root of your project in order for the InteliSense to understand that you are indeed inside a JavaScript project and to link the files together rather than treating them as separate entities. See details on jsconfig.json in VS Code documentation.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论