Visual Studio Code 中的 JQuery 智能感知

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33902077/
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 16:58:31  来源:igfitidea点击:

JQuery intellisense in Visual Studio Code

jqueryideintellisensevisual-studio-code

提问by grmbl

I decided to jump in Visual Studio Code to create an app.
Now I can't seem to get intellisense working for both JQuery and JQuery Mobile.

我决定跳入 Visual Studio Code 来创建一个应用程序。
现在我似乎无法让智能感知同时适用于 JQuery 和 JQuery Mobile。

My VSC version is 0.10.1

我的 VSC 版本是 0.10.1

I tried adding a reference.d.tsfile with this contents

我尝试添加reference.d.ts包含此内容的文件

/// <reference path="js/jquery.d.ts"/>
/// <reference path="js/jquerymobile.d.ts"/>

(files downloaded from https://github.com/DefinitelyTyped/DefinitelyTyped)

(从https://github.com/DefinitelyTyped/DefinitelyTyped下载的文件)

I tried opening a .jsfile, add a $(this)and hitting ctrl+period but it says 'No suggestions'.

我尝试打开一个.js文件,添加一个$(this)并按 ctrl+period 但它说“没有建议”。

Something I missed?

我错过了什么?

UPDATE:

更新:

Tried adding a jsconfig.jsonfile following https://code.visualstudio.com/docs/languages/javascript#_javascript-projects-jsconfigjson

尝试jsconfig.json按照https://code.visualstudio.com/docs/languages/javascript#_javascript-projects-jsconfigjson添加文件

{
    "files": [
        "js/jquery.d.ts",
        "js/jquerymobile.d.ts"
    ]
}

Restarted, nothing...

重启,什么都没有……

UPDATE 2:

更新 2:

added a .jsfile, typed $and clicked on the lightbulb to download jquery.d.ts file. It warns me about adding this file to the jsconfig.json file. After doing all this, restarted still not working.

添加了一个.js文件,键入$并单击灯泡以下载 jquery.d.ts 文件。它警告我将此文件添加到 jsconfig.json 文件。做完这一切,重启还是不行。

jsconfig.jsonfile contents:

jsconfig.json文件内容:

{
    "files": [
        "typings/jquery/jquery.d.ts"
    ]
}

Nothing!

没有!

回答by oldbam

Latest (as of April 2017) suggested approach to do this is to use automatic type acquisition by adding jsconfig.jsonfile in the root of your project with the following contents:

最新的(截至 2017 年 4 月)建议的方法是通过jsconfig.json在项目的根目录中添加具有以下内容的文件来使用自动类型获取:

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

Then, in your editor you will see:

然后,在您的编辑器中,您将看到:

jquery intellisense in Visual Studio Code

Visual Studio Code 中的 jquery 智能感知

For more information, check official documentation

更多信息请查看官方文档

回答by Willjobs

If you are already familiar with npm (if not you should)

如果您已经熟悉 npm(如果不熟悉,则应该)

I suggest you install typingswhich is a typeScript definition manager that work very well with Visual Studio Code (Tested on Mac and Windows)

我建议你安装分型这是一种打字稿定义管理者的工作非常好,Visual Studio代码(测试Mac和Windows)

Simply run

只需运行

npm install typings --global

Of course you must need to have npm already installedon your local pc/mac.

当然,你必须已经在本地 pc/mac 上安装了npm 。

After that, move to the command line (terminal or cmd) and run

之后,移动到命令行(终端或cmd)并运行

typings install dt~jquery --global --save

It will create the typings directory and all required files (not need to restart Visual Studio Code) and you are ready to go!.

它将创建 Typings 目录和所有必需的文件(不需要重新启动 Visual Studio Code),您就可以开始了!

If you want to know more about typings just follow the typings link above.

如果您想了解有关打字的更多信息,只需按照上面的打字链接即可。

回答by insCode

step 1 : make sure you have the definition files in your project if you got npm installed then run

第 1 步:如果您安装了 npm,请确保您的项目中有定义文件,然后运行

npm i --save-dev @types/jquery

it will install jQuery definition file in node_modules/@types/jquery else : download it. and put it in any folder then , step:2 create a jsconfig.json file

它将在 node_modules/@types/jquery else 中安装 jQuery 定义文件:下载它。然后把它放在任何文件夹中,步骤:2 创建一个 jsconfig.json 文件

$ touch jsconfig.json

and put this code in the jsconfig.json file

并将此代码放入 jsconfig.json 文件中

{
    "include": [
        "./node_modules/@types"
    ],
    "exclude": [
        "./node_modules"
    ]
}

that's it .

就是这样 。

**

**

note : i am excluding ./node_modules in case you have it in your project if not then just include the jquery.d.ts file .

注意:我不包括 ./node_modules 以防万一你的项目中有它,如果没有,那么只包含 jquery.d.ts 文件。

**

**

thanks

谢谢

回答by Tomz Re

I had a little bit problem with it, so I would like to share the solution that worked for me. At first you need in project root package.json if you don't have it in console simply type:

我有一点问题,所以我想分享对我有用的解决方案。首先你需要在项目根 package.json 中,如果你在控制台中没有它,只需输入:

$ npm init

this command should create a package.json with necessary content in it. Then run this command in console:

此命令应创建一个包含必要内容的 package.json。然后在控制台中运行此命令:

$ npm install --save @types/jquery

and like @oldbam said before use automatic type acquisition: by creating jsonconfig.json with code below:

就像@oldbam 之前所说的使用自动类型获取:通过使用以下代码创建 jsonconfig.json:

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

回答by Eric Clinton

You shouldn't forget to add the reference to the d.ts file in your scripts. For eg. add this reference /// <reference path="js/jquery.d.ts"/>at the top of the script you want to see the jquery suggestions as you type.

您不应忘记在脚本中添加对 d.ts 文件的引用。例如。/// <reference path="js/jquery.d.ts"/>在您希望在键入时查看 jquery 建议的脚本顶部添加此引用。

回答by Hymany

if you are using typings 1.x, you should use:

如果您使用类型 1.x,您应该使用:

typings install dt~jquery --global

打字安装 dt~jquery --global

in typing 1.x, there's much changes .

在输入 1.x 时,有很多变化。

回答by bugs_cena

under typings/directory create a file called "tsd.d.ts" with the following content:

typings/目录下创建一个名为“tsd.d.ts”的文件,内容如下:

/// <reference path="js/jquery.d.ts"/>
/// <reference path="js/jquerymobile.d.ts"/>

jsconfig.json need not contain the files attribute:

jsconfig.json 不需要包含 files 属性:

{
    "compilerOptions": {
        "target": "ES5",
    }
}