简体   繁体   English

如何在Visual Studio Code中对库使用三斜杠引用?

[英]How to use triple-slash references for libraries in Visual Studio Code?

I'm trying to get Visual Studio Code 0.3.0 to recognize my JavaScript libraries. 我正在尝试使用Visual Studio Code 0.3.0来识别我的JavaScript库。 However, intellisense is complaining. 然而,intellisense正在抱怨。 Here's a repro: 这是一个复制品:

  1. Open Visual Studio Code 打开Visual Studio代码
  2. File > Open Folder (select a freshly created empty folder) 文件>打开文件夹(选择一个新创建的空文件夹)
  3. Add a file mytest.js to that folder 将文件mytest.js添加到该文件夹
  4. Download jquery-2.1.4.js (full/uncompressed) to that folder (appears in the side bar) jquery-2.1.4.js (完整/未压缩)下载到该文件夹​​(显示在侧栏中)
  5. Enter this in mytest.js : mytest.js输入:

     var x = jQuery('body'); 

Result: squiggly green lines under jQuery . 结果: jQuery下的曲线绿线。 Note that jQuery is just an example, I've had the same issue with libraries such as KnockoutJS and QUnit, etc. 请注意,jQuery只是一个例子,我对KnockoutJS和QUnit等库有同样的问题。

I've tried adding a triple-slash reference like so: 我试过添加一个三斜杠引用,如下所示:

/// <reference path="jquery-2.1.4.js" />

Heck, it even autocompleted the path for me. 哎呀,它甚至自动完成了我的path I've tried varying the path a bit, eg a ./ at the start, but the result is thus far invariably: 我尝试过改变一下路径,例如一开始的./ ,但结果总是如此:

带有波浪线的最终产品的屏幕截图

Hovering jQuery gives a popup saying: 悬停jQuery给出一个弹出窗口说:

Cannot find name 'jQuery'. 找不到名字'jQuery'。
any

Still squiggly green lines. 仍然曲折的绿线。 I hate squiggly lines though. 讨厌潦草的线条。 How can I get rid of them? 我怎么能摆脱他们?

You Need To Refefence the jQuery TypeScript Definition File. 您需要更新jQuery TypeScript定义文件。


You need a 'typings' folder in the root of your app or site. 您需要在应用或网站的根目录中使用“typings”文件夹 Within the 'typings' folder you need a jquery.d.ts file . 在'typings'文件夹中,您需要一个jquery.d.ts文件

Your reference to the file should be similar to the following depending upon where the file reference is located in relation to the typings/jquery.d.ts file and folder: 您对该文件的引用应类似于以下内容,具体取决于文件引用相对于typings / jquery.d.ts文件和文件夹的位置:

/// <reference path="../../typings/jquery/jquery.d.ts"/>

Here's a TypeScript Definitions File reference for Node.js: 这是Node.js的TypeScript定义文件参考:

/// <reference path="typings/node/node.d.ts"/>

The easiest way to accomplish this is to click on the green squiggly in VSCode then click the light bulb and select Add /// reference to 'XYZ.d.ts' . 完成此操作的最简单方法是单击VSCode中的绿色波浪形,然后单击light bulb并选择Add /// reference to 'XYZ.d.ts' This will automatically add everything you need. 这将自动添加您需要的一切。

In a comment above the Definitely Typed web site was referenced if you want or need to do this manually. 在上面的注释中,如果您需要或需要手动执行此操作,则会引用绝对类型的网站。

I don't know about VS Code, but regular Visual Studio often complains when you try to access "global" variables like this. 我不知道VS代码,但是当你试图访问像这样的“全局”变量时,常规的Visual Studio经常会抱怨。 I find that this pattern helps me to both avoid these warnings and keep my code more modular: 我发现这种模式可以帮助我避免这些警告,并使我的代码更加模块化:

(function($) {
    var x = $('body');
})(jQuery); 

This has the added advantages of keeping your declared variables ( x , eg) scoped within the file instead of globally, and allowing you to safely use short names (eg $ ) for your libraries internally, while avoiding naming conflicts. 这样做的另一个好处是可以将声明的变量(例如x )保存在文件中而不是全局,并允许您在内部安全地使用短名称(例如$ ),同时避免命名冲突。 It also opens up an easy migration path if you end up needing to use requireJS or something like that for dependency loading. 如果您最终需要使用requireJS或类似的东西来进行依赖性加载,它还会打开一个简单的迁移路径。

Hopefully it will also get rid of your green squigglies? 希望它也能摆脱你的绿色波浪形?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 打字稿。使用“import from”时仍需要Triple-Slash引用 - Typescript. Are Triple-Slash references still required when using “import from” 打字稿对HTML文件的三斜杠指令引用 - typescript Triple-Slash Directives reference to html file 如何在Visual Studio 2008的.ASPX页中的Javascript代码中使用断点 - How to use breakpoints in Javascript code in .ASPX page of Visual Studio 2008 如何使用正则表达式在 Visual Studio Code 的代码片段中“大写和替换”? - How to use Regex to 'capitalize and replace' in Visual Studio Code's snippets? 如何使 Visual Studio Code 使用路径映射进行自动导入? - How to make Visual Studio Code use path mappings for automatic imports? 如何在带有 webview 的 visual studio 代码扩展中使用 axios - How to use axios in visual studio code extensions with webview JavaScript库以及Visual Studio - JavaScript libraries along with Visual Studio 如何在 Visual Studio Code 中更改代码格式 - How to change code formatting in Visual Studio Code 如何在Visual Studio代码中粘贴代码格式 - How to paste the code format in visual studio code Visual Studio代码自动完成似乎不适用于外部库上的JavaScript,例如glmatrix - Visual Studio Code autocompletion doesn't seem to work for JavaScript on foreign libraries e.g. glmatrix
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM