VS Code 中的 jQuery 智能感知

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41777327/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 17:16:55  来源:igfitidea点击:

jQuery intellisense in VS Code

jqueryvisual-studio-codeintellisense

提问by LunielleDev

I have tried this:

我试过这个:

JQuery intellisense in Visual Studio Code

Visual Studio Code 中的 JQuery 智能感知

and this:

和这个:

http://shrekshao.github.io/2016/06/20/vscode-01/

http://shrekshao.github.io/2016/06/20/vscode-01/

But it does nothing, VS Code just won't add jquery intellisense, I've been trying to solve this for hours but it just won't work

但它什么也没做,VS Code 只是不会添加 jquery 智能感知,我已经尝试解决这个问题好几个小时了,但它不起作用

回答by kwood

Most of the blog postings are now outdated, as we finally have automatic type acquisition with version 1.8+ - you no longer need to install the typings yourself.

大多数博客帖子现在已经过时了,因为我们终于在 1.8+ 版本中实现了自动类型获取 - 您不再需要自己安装类型。

I recommend reading the official documentation, its always up to date: https://code.visualstudio.com/docs/languages/javascript

我建议阅读官方文档,它总是最新的:https: //code.visualstudio.com/docs/languages/javascript

If you use npm and have a package.jsonin your project and jQuery is listed there, it should already work.

如果你使用 npm 并且package.json在你的项目中有一个jQuery 列在那里,它应该已经可以工作了。

If you do not use npm, you can create the file jsconfig.jsonin the project root with the following content and you are good to go:

如果您不使用 npm,您可以jsconfig.json在项目根目录中创建包含以下内容的文件,您就可以开始了:

{
    "typeAcquisition": {
        "include": [
            "jquery"
        ]
    }

}

回答by Wong Jia Hau

Type this command in your project root :

在您的项目根目录中键入此命令:

npm i --save @types/jquery

回答by Ben

I had the same problem and google brought me here. I added the type jsconfig.json and "typeAcquisition" and still nothing.

我遇到了同样的问题,谷歌把我带到了这里。我添加了类型 jsconfig.json 和“typeAcquisition”,但仍然没有。

Turns out you have to have node and npm installed. Even if you are not using them for package management and are importing jquery from a CDN.

原来你必须安装 node 和 npm。即使您没有将它们用于包管理并且是从 CDN 导入 jquery。

From the Docs

来自文档

Many popular libraries ship with typings files so you get IntelliSense for them automatically. For libraries that do not include typings, VS Code's Automatic Type Acquisition will automatically install community maintained typings file for

Automatic type acquisition requires npmjs, the Node.js package manager, which is included with the Node.js runtime. In this image you can see IntelliSense, including the method signature, parameter info, and the method's documentation for the popular lodash library.

许多流行的库都附带了类型文件,因此您可以自动获得它们的 IntelliSense。对于不包含类型的库,VS Code 的自动类型获取将自动安装社区维护的类型文件

自动类型获取需要 npmjs,即 Node.js 包管理器,它包含在 Node.js 运行时中。在此图像中,您可以看到 IntelliSense,包括方法签名、参数信息和流行的 lodash 库的方法文档。

https://code.visualstudio.com/docs/nodejs/working-with-javascript

https://code.visualstudio.com/docs/nodejs/working-with-javascript

So vs code uses npm for auto type acquisition.

所以 vs code 使用 npm 进行自动类型获取。

May be super basic, but it solved my problem so I hope it helps someone else too.

可能是超级基本的,但它解决了我的问题,所以我希望它也能帮助其他人。

I also used the configuration in jsconfig.json as described by kwood. Not sure I needed to specify it manually after installin npm but it is working so i'm not asking questions

我还使用了 kwood 描述的 jsconfig.json 中的配置。不确定我是否需要在安装 npm 后手动指定它,但它正在工作,所以我没有问问题

回答by user8805240

I thought that the major reason is Vscode does not parse the Jquery.js file because original jquery file is minified, which leads to vscode stop tokenization of the file. To solve this, open the jquery.js file, right click and select format. The tokenization process will complete.

我认为主要原因是由于原始jquery文件被缩小了,vscode没有解析jquery.js文件,这导致vscode停止文件的标记化。要解决此问题,请打开 jquery.js 文件,右键单击并选择格式。令牌化过程将完成。