我们如何使用Typescript创建自定义的jquery插件? [英] How can we create a custom jquery plugin using typescript?

查看:152
本文介绍了我们如何使用Typescript创建自定义的jquery插件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在我的应用程序中使用jquery自定义插件,如何使用Typescript创建自定义jquery插件.我已经在Google上搜索了很多,而我只是在以下链接

I need to use a jquery custom plugin in my application and how can i create a custom jquery plugin using typescript. I have googled a lot and i just got below links

http://typescript.codeplex.com/discussions/398401

在TypeScript中定义自定义jQuery UI小部件

上面的链接使用普通的javascript代码,但是javascript代码将在typescript中工作,但是问题是我不能在此插件中使用typescript类. 有没有办法在jquery插件中使用Typescript类,否则有没有其他办法在Typescript中创建jquery插件.

The above links uses plain javascript code, however javascript code will work in typescript but the issue with this is i can't use typescript classes with in this plugin. Is there any way to use typescript classes with in jquery plugin or else is there any other way to create jquery plugin in typescript.

任何建议都应得到赞赏.

Any suggestions should be appreciated.

推荐答案

在创建插件时,使用JavaScript和Typescript之间几乎没有区别.您可能想从此处抢先获得TypeScript的jQuery定义将其添加到您的项目中(或将其包含在命令行等中,具体取决于您的开发环境).

There's very little difference between using JavaScript and Typescript when creating a plugin. You'll likely want to grab the jQuery definition for TypeScript from here first and add it to your project (or include it on the command line, etc., depending on your development environment).

然后,只需编写插件并使用所需的任何类和现有代码即可.下面的示例除了证明与原始JavaScript插件技术几乎没有什么不同外,没有做任何其他事情.

Then, just write the plugin and use whatever classes and existing code you want. The example below doesn't do anything other than demonstrate that it's hardly any different from the original JavaScript plugin techniques.

(function($) {
    $.fn.myPlugin = function() {
        // do something
        return this;
    };

    $.fn.myEachPlugin = function(options)  {
        var settings: any = $.extend({
            color: "#ffff00"
        }, options);

        return this.each(function() {
            // do something ...
        });
    };

})(jQuery);

这篇关于我们如何使用Typescript创建自定义的jquery插件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆