VS Code 转到实现 / 定义(也称为 F12,也称为 CTRL+单击)不起作用。

huangapple go评论89阅读模式
英文:

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.jsontsconfig.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.

huangapple
  • 本文由 发表于 2023年2月18日 16:53:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/75492210.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定