简体   繁体   English

在Typescript中导入jQuery和其他库

[英]Import jQuery & other libraries in Typescript

after days of research and struggeling I have to ask here to fine someone to clearify the process of importing a library in Typescript . 经过数天的研究和奋斗,我必须在这里请求罚款, 以澄清用Typescript导入图书馆的过程

I was used to just add the tag and everything works fine. 我曾经只是添加标签,然后一切正常。 Now that I´m trying to build a MEAN-Stack application I don´t figure out how that all works with the definition files and such... 现在,我正在尝试构建MEAN-Stack应用程序,现在我不知道它们如何与定义文件等一起使用。

WHAT I TRIED SO FAR: 我尝试过的很远:

npm install jquery //terminal root: /project/angular-src
npm install @types/jquery //same directory here

then I importet jQuery in the one of my .ts files 然后在我的.ts文件之一中导入jQuery

import * as $ from 'jquery'; 

I tried out several variations of this ( installing jQuery and @types/jQuery in the project folder instead of the angular-src folder) but none of it worked for me. 我尝试了几种方法(将jQuery和@ types / jQuery安装在项目文件夹中,而不是angular-src文件夹中),但是对我来说都没有用。 I thought it would be that simple but maybe I´m missing something out here. 我以为就这么简单,但也许我在这里遗漏了一些东西。 I always get some kind of errors like "Generic type 'xyz' requires x arguments" ( like 200 rows of these ) 我总是会遇到诸如“泛型'xyz'需要x参数”之类的错误(例如其中200行)

Hope you can help me explain why this doesnt work and how I can make it work 希望您能帮助我解释为什么这种方法不起作用以及如何使它起作用

PS: I found similar questions here before but I didnt really find them helpful as I tried them out with no success PS:我以前在这里发现过类似的问题,但是我没有真正发现它们有帮助,因为我没有成功尝试过

You can, in fact, use jQuery with TypeScript. 实际上,您可以将jQuery与TypeScript一起使用。 You will need to download the jQuery definition file located here . 您将需要下载位于此处的jQuery定义文件。

With that downloaded, copy the definition file (under the v2 folder, 'index.d.ts' file) to your solution/project. 下载后,将定义文件(在v2文件夹下,“ index.d.ts”文件下)复制到您的解决方案/项目中。

Once added to your project, you can reference the jQuery definition file by adding this to the top of your .ts file: 一旦添加到项目中,您就可以通过将jQuery定义文件添加到.ts文件的顶部来引用它:

///<reference path="../{your-location}/jquery.d.ts"/>

Which should enable jQuery in your TypeScript app. 应该在您的TypeScript应用程序中启用jQuery。

JQuery和Angular是使用Angular或JQuery的两个独立的事物。

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

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