简体   繁体   English

TypeScript和库,例如jQuery(带.d.ts文件)

[英]TypeScript and libraries such as jQuery (with .d.ts files)

I've looked all over the internets but I have yet to find a comprehensive guide that tells me how to take a library such as jquery and use it in a TypeScript project. 我已经查看了整个互联网,但我还没有找到一个全面的指南,它告诉我如何使用像jquery这样的库并在TypeScript项目中使用它。 If there is a guide that exists I would love to know where otherwise these are the things I really need to know: 如果有一个指南存在,我很想知道在哪里这些是我真正需要知道的事情:

  1. I understand that the .d.ts file is only for intellisense so what do I need to get jquery to actually work (compile to ts?) 我知道.d.ts文件只适用于intellisense所以我需要让jquery实际工作(编译成ts?)
  2. Do I need a requires or a //reference when using VS2013 and if so what is that actually doing? 使用VS2013时是否需要requires//reference ?如果是,那实际上是做什么的?
  3. Anything to go from these .d.ts and jquery js files to using the library (or any library) in my ts project. 从这些.d.ts和jquery js文件到我的ts项目中使用库(或任何库)的任何东西。

I've been able to figure pretty much everything else out but this one has been rather frustrating. 我已经能够解决其他所有问题,但这个问题令人非常沮丧。

You don't need to compile jquery to typescript, you just need to use a definition file that tells Typescript how the JavaScript library works. 您不需要将jquery编译为typescript,您只需要使用一个定义文件来告诉Typescript JavaScript库的工作方式。

Get definitions here: https://github.com/DefinitelyTyped/DefinitelyTyped 在此处获取定义: https//github.com/DefinitelyTyped/DefinitelyTyped

or from NuGet if using Visual Studio. 或者如果使用Visual Studio,则从NuGet获取。

Then just write your typescript as normal, and declare your library if needed: 然后正常编写您的打字稿,并在需要时声明您的库:

declare var library : libraryTypedName

for example jquery's d.ts file already does this for you (check the bottom): 例如jquery的d.ts文件已经为你做了这个(查看底部):

declare module "jquery" {
    export = $;
}
declare var jQuery: JQueryStatic;
declare var $: JQueryStatic;

https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/jquery https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/jquery

Now in your .ts file when you type $ it should give you the typescript intellisense. 现在在你的.ts文件中键入$它应该为你提供typescript intellisense。

Now the only things you want to include in your bundleconfig / <script> are the .js files, both yours and jquery / other libraries. 现在,您希望在bundleconfig / <script>中包含的唯一内容是.js文件,包括您的文件和jquery /其他库。 Typescript is COMPILE time only! 打字稿只是COMPILE时间!

The convention in TypeScript is to have a reference.ts or reference.d.ts file in your project that will bring in these external references. TypeScript中的约定是在项目中有一个reference.tsreference.d.ts文件,它将引入这些外部引用。

So in your reference.ts file add the path to your jquery.d.ts (the path will be relative to the reference.ts file): 所以在你的reference.ts文件中添加你的jquery.d.ts的路径(该路径将相对于reference.ts文件):

/// <reference path="../relative/path/to/jquery.d.ts"/>

Then you should be able to use the jQuery definitions in your project. 然后,您应该能够在项目中使用jQuery定义。

NOTE: The reference.ts file is a convention but you can actually add a <reference path="..."/> directive to any TypeScript file if needed. 注意:reference.ts文件是一种约定,但如果需要,您实际上可以将<reference path="..."/>指令添加到任何TypeScript文件中。


From the TypeScript Language Specificiation (PDF) 11.1.1: TypeScript语言规范(PDF) 11.1.1:

A comment of the form /// <reference path="…"/> adds a dependency on the source file specified in the path argument. /// <reference path="…"/>形式的注释添加了对path参数中指定的源文件的依赖性。 The path is resolved relative to the directory of the containing source file. 相对于包含源文件的目录解析路径。

I am using it in VS 2015, and I am new to typescript.I used jQuery and leaflet in my project. 我在VS 2015中使用它,我是typescript的新手。我在我的项目中使用了jQuery和leaflet。 My solution is: 我的解决方案是:

  1. Download all these libraies from NuGet manager in VS 2015. 从VS 2015中的NuGet经理下载所有这些图书馆。 在此输入图像描述

  2. Drag the library files (.js) as instructed in this tutorial: 按照本教程中的说明拖动库文件(.js):

https://taco.visualstudio.com/en-us/docs/get-started-first-mobile-app/ https://taco.visualstudio.com/en-us/docs/get-started-first-mobile-app/

  1. Modify the index.html file by adding these lines. 通过添加这些行来修改index.html文件。

     <link rel="stylesheet" href="css/jquery.mobile-1.4.5.min.css" /> <link rel="stylesheet" href="css/leaflet.css" /> <script src="scripts/jquery-2.2.3.min.js"></script> <script src="scripts/jquery.mobile-1.4.5.min.js"></script> <script src="scripts/leaflet-0.7.3.min.js"></script> 
  2. Modify the index.ts file. 修改index.ts文件。 First add these lines on top to reference your libraries. 首先在顶部添加这些行以引用您的库。 You may need to change the path. 您可能需要更改路径。

    在此输入图像描述

  3. Add your won code within onDeviceReady(), otherwise you might get javascript runtime error, like sysmbol "$" is not defined. 在onDeviceReady()中添加您赢得的代码,否则您可能会收到javascript运行时错误,如sysmbol“$”未定义。 I guess this is because the codes try to load some function when the device is not ready yet. 我想这是因为当设备尚未准备好时,代码会尝试加载某些功能。 This has worked for me. 这对我有用。 Hope it would help you too. 希望它也能帮到你。

      function onDeviceReady() { document.addEventListener('pause', onPause, false); document.addEventListener('resume', onResume, false); var parentElement = document.getElementById('deviceready'); var listeningElement = parentElement.querySelector('.listening'); var receivedElement = parentElement.querySelector('.received'); listeningElement.setAttribute('style', 'display:none;'); receivedElement.setAttribute('style', 'display:block;'); // your code goes here } 

this solution is not for typescript purists :), but if you prefer one line solution, just add this line to your .ts file: 此解决方案不适用于打字稿纯粹主义者:),但如果您更喜欢一行解决方案,只需将此行添加到.ts文件中:

declare var $: any

and then use $ anywhere in your typescript code as if you were in js code. 然后在你的打字稿代码中使用$ ,就像你在js代码中一样。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM