gpt4 book ai didi

jquery - 如何使用 Typescript 通过 AMD 模块加载 Jquery 插件

转载 作者:搜寻专家 更新时间:2023-10-30 21:25:10 25 4
gpt4 key购买 nike

我可以像这样使用 amd 模块加载 jQuery - require jquery via AMD in TypeScript我也了解插件如何在没有模块的情况下修改 jquery 接口(interface) - Using jQuery plugin in TypeScript

但是如何通过 AMD 模块加载 jQuery 插件呢?并让它更新 jQuery 界面?

import plugin1 = module("jquery.tmpl");
import jQuery= module("jquery");

plugin1 和 jQuery 都会有不同版本的 jQuery 接口(interface),plugin1 不会添加到 jquery 模块中定义的接口(interface)。

我试过将每个文件中的模块命名为相同的

export module jQuery {
// .., jQuery definitions
}

还是不行

最佳答案

将以下内容添加到 github/definetelytyped 上的 jquery.d.ts 文件末尾.

declare module "jquery"{ 
/****
AJAX
*****/
export function ajax(settings: JQueryAjaxSettings): JQueryXHR;
export function ajax(url: string, settings?: JQueryAjaxSettings): JQueryXHR;

export function ajaxPrefilter(dataTypes: string, handler: (opts: any, originalOpts: any, jqXHR: JQueryXHR) => any): any;
export function ajaxPrefilter(handler: (opts: any, originalOpts: any, jqXHR: JQueryXHR) => any): any;

export var ajaxSettings: JQueryAjaxSettings;

export function ajaxSetup(options: any);

export function get(url: string, data?: any, success?: any, dataType?: any): JQueryXHR;
export function getJSON(url: string, data?: any, success?: any): JQueryXHR;
export function getScript(url: string, success?: any): JQueryXHR;

export function param(obj: any): string;
export function param(obj: any, traditional: bool): string;

export function post(url: string, data?: any, success?: any, dataType?: any): JQueryXHR;

/*********
CALLBACKS
**********/
export function Callbacks(flags?: string): JQueryCallback;

/****
CORE
*****/
export function holdReady(hold: bool): any;
export function (selector: string, context?: any): JQuery;
export function (element: Element): JQuery;
export function (object: { }): JQuery;
export function (elementArray: Element[]): JQuery;
export function (object: JQuery): JQuery;
export function (func: Function): JQuery;
export function (array: any[]): JQuery;
export function (): JQuery;

export function noConflict(removeAll?: bool): Object;

export function when(...deferreds: any[]): JQueryPromise;

/***
CSS
****/
export function css(e: any, propertyName: string, value?: any);
export function css(e: any, propertyName: any, value?: any);
export var cssHooks: { [key: string]: any; };
export var cssNumber: any;

/****
DATA
*****/
export function data(element: Element, key: string, value: any): any;
export function data(element: Element, key: string): any;
export function data(element: Element): any;

export function dequeue(element: Element, queueName?: string): any;

export function hasData(element: Element): bool;

export function queue(element: Element, queueName?: string): any[];
export function queue(element: Element, queueName: string, newQueueOrCallback: any): JQuery;

export function removeData(element: Element, name?: string): JQuery;

/*******
EFFECTS
********/
export var fx: { tick: () => void; interval: number; stop: () => void; speeds: { slow: number; fast: number; }; off: bool; step: any; };

/******
EVENTS
*******/
export function proxy(context: any, name: any): any;
export function Deferred(): JQueryDeferred;

/*********
INTERNALS
**********/
export function error(message: any);

/*************
MISCELLANEOUS
**************/
export var expr: any;
export var fn: any; //TODO: Decide how we want to type this
export var isReady: bool;

/**********
PROPERTIES
***********/
export var browser: JQueryBrowserInfo;
export var support: JQuerySupport;

/*********
UTILITIES
**********/
export function contains(container: Element, contained: Element): bool;

export function each(collection: any, callback: (indexInArray: any, valueOfElement: any) => any): any;

export function extend(target: any, ...objs: any[]): Object;
export function extend(deep: bool, target: any, ...objs: any[]): Object;

export function globalEval(code: string): any;

export function grep(array: any[], func: any, invert: bool): any[];

export function inArray(value: any, array: any[], fromIndex?: number): number;

export function isArray(obj: any): bool;
export function isEmptyObject(obj: any): bool;
export function isFunction(obj: any): bool;
export function isNumeric(value: any): bool;
export function isPlainObject(obj: any): bool;
export function isWindow(obj: any): bool;
export function isXMLDoc(node: Node): bool;

export function makeArray(obj: any): any[];

export function map(array: any[], callback: (elementOfArray: any, indexInArray: any) =>any): JQuery;

export function merge(first: any[], second: any[]): any[];

export function noop(): any;

export function now(): number;

export function parseJSON(json: string): Object;

//FIXME: This should return an XMLDocument
export function parseXML(data: string): any;

export function queue(element: Element, queueName: string, newQueue: any[]): JQuery;

export function trim(str: string): string;

export function type(obj: any): string;

export function unique(arr: any[]): any[];
}

然后只需使用:

import $ = module("jquery");

这假定您的 jquery js 文件位于同一目录中并命名为 jquery.js。您可以在 my blog 的下载部分看到我的示例。 .

关于jquery - 如何使用 Typescript 通过 AMD 模块加载 Jquery 插件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13529010/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com