简体   繁体   English

在 Typescript 中使用 JQuery 插件

[英]Using JQuery Plugins With Typescript

I've got a JQuery plugin (mixItUp) that I'm trying to use with Typescript, which doesn't have it's own *.d.ts file.我有一个 JQuery 插件 (mixItUp),我试图将它与 Typescript 一起使用,它没有自己的 *.d.ts 文件。

How can I make it work with typescript?我怎样才能让它与打字稿一起工作?

I've made my own mixItUp.d.tds file with the following content:我制作了自己的 mixItUp.d.tds 文件,内容如下:

interface JQuery
{
    mixItUp():any;
}

In the module that I'm trying to use mixItUp in, I am using the following code:在我尝试使用 mixItUp 的模块中,我使用了以下代码:

$('#mySelector').mixItUp();

It compiles fine, and I get intellisense for mixItUp, so somehow it's picked up the mixItUp.d.ts file without me referencing it.它编译得很好,我得到了 mixItUp 的智能感知,所以不知何故它在没有我引用的情况下拿起了 mixItUp.d.ts 文件。

In the html, I'm loading in jquery, then the non-typescript (Pure js) mixItUp library before this module, so I know both jquery and mixitup really exists at that point in time.在 html 中,我在 jquery 中加载,然后在这个模块之前加载非打字稿(纯 js)mixItUp 库,所以我知道 jquery 和 mixitup 在那个时间点确实存在。

However, when I load the page I get the following error message:但是,当我加载页面时,我收到以下错误消息:

$(...).mixItUp is not a function(…)

Most of the guides I'm finding seem to be using what I understand to be the old way of referencing *.d.ts files.我发现的大多数指南似乎都在使用我理解的引用 *.d.ts 文件的旧方法。

Can someone please explain the correct way to use JQuery plugins with typescript, as well as if possible how to reference non typescript libraries (I assume the technique will be similar).有人可以解释使用带有打字稿的 JQuery 插件的正确方法,以及如果可能的话如何引用非打字稿库(我认为该技术将是相似的)。

Thanks.谢谢。

If it's happening at runtime, it's most likely not caused by Typescript, since TS just gets compiled to JS.如果它发生在运行时,它很可能不是由 Typescript 引起的,因为 TS 只是被编译为 JS。 Since TS itself has no knowledge of your library and only gets fed the information about it by your interface, it compiles that call as-is(which can be proven, by just looking at the output in the Developer console or similar).由于 TS 本身不了解您的库,并且只能通过您的界面获取有关它的信息,因此它会按原样编译该调用(只需查看开发人员控制台或类似工具中的输出即可证明这一点)。

So make sure mixItUp is loaded correctly, before your code is executed(but after jQuery) and it should work fine(given that the jQuery version you're using is supported by the plugin)因此,请确保在执行代码之前(但在 jQuery 之后)正确加载 mixItUp 并且它应该可以正常工作(假设插件支持您使用的 jQuery 版本)

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

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