转到 Visual Studio 代码中的定义不起作用 [英] Go to definition in visual studio code does not work

查看:33
本文介绍了转到 Visual Studio 代码中的定义不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试按照 https://code 为 nodejs 项目设置 Visual Studio 代码.visualstudio.com/docs/languages/javascript

我在包含内容的根文件夹中创建了一个 jsconfig.json 文件

<代码>{编译器选项":{"target": "ES5",//也试过 ES6模块":commonjs"}}

<块引用>

这个文件告诉 VS Code 您正在编写符合 ES5 的代码,并且您要使用的模块系统是 commonjs 框架.设置这些选项后,您可以开始编写引用其他文件中的模块的代码.例如,在 app.js 中,我们需要 ./routes/index 模块,该模块导出 Express.Router 类.如果在路由上调出 IntelliSense,则可以看到 Router 类的形状.

虽然它似乎不适用于 vscode 0.9.1.我没有在我自己的模块上获得智能感知.转到定义也不起作用.

https://code.visualstudio.com/docs/runtimes/nodejs#_great-code-editing-experiences

有没有办法让定义工作?

解决方案

TLDR;如果您直接在 exports 对象上设置变量/函数,它似乎可以工作.


在尝试了不同的方式来编写模块后,我发现如果你以某种方式编写模块是可行的.这是一个有效的例子.

exports.myFunction = function (a, b) {返回 a + b;};

现在,如果您从另一个文件导入此模块,您将获得智能感知并转到定义将起作用.但是,如果您像这样编写模块或任何其他变体,它将无法正常工作.

var myModule = {};myModule.myFunction = 函数 (a,b) {返回 a + b;};出口 = myModule;

在 vscode 存储库上提交了一个问题.

https://github.com/Microsoft/vscode/issues/15004#issuecomment-258980327

如果情况发生变化,我会更新我的答案.

I am trying to setup visual studio code for a nodejs project following https://code.visualstudio.com/docs/languages/javascript

I created a jsconfig.json file in my root folder with the contents

{
  "compilerOptions": {
    "target": "ES5", //tried ES6 as well
    "module": "commonjs"
  }
}

This file tells VS Code you are writing ES5 compliant code and the module system you want to use is the commonjs framework. With these options set, you can start to write code that references modules in other files. For example, in app.js we require the ./routes/index module, which exports an Express.Router class. If you bring up IntelliSense on routes, you can see the shape of the Router class.

Although it doesn't seem to work with vscode 0.9.1. I don't get intellisense on my own modules. Go to definition doesn't work either.

https://code.visualstudio.com/docs/runtimes/nodejs#_great-code-editing-experiences

Is there a way to get go to definition working?

解决方案

TLDR; if you set your variables/functions directly on the exports object it seems to work.


After trying different ways to write modules I found out it works if you write your module in a certain way. Here is an example that works.

exports.myFunction = function (a, b) {
    return a + b;
};

Now if you import this module from another file you will get intellisense and go to definition will work. However if you write your module like this or any other variation it will not work.

var myModule = {};
myModule.myFunction = function (a,b) {
    return a + b;
};
exports = myModule;

Filed an issue on the vscode repository.

https://github.com/Microsoft/vscode/issues/15004#issuecomment-258980327

I will update my answer if this changes.

这篇关于转到 Visual Studio 代码中的定义不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆