[英]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: 这是一个复制品:
mytest.js
to that folder 将文件mytest.js
添加到该文件夹 jquery-2.1.4.js
(full/uncompressed) to that folder (appears in the side bar) 将jquery-2.1.4.js
(完整/未压缩)下载到该文件夹(显示在侧栏中) 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.