如何在 TypeScript 中通过 AMD 要求 jquery [英] How to require jquery via AMD in TypeScript

查看:26
本文介绍了如何在 TypeScript 中通过 AMD 要求 jquery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 TypeScript 模块如何需要 jquery AMD 模块.例如,假设脚本的目录结构如下所示:

<前>jquery-1.8.2.jsjquery.d.ts模块.ts需要.js

我希望从 module.ts 生成的 js 文件要求通过 require.js 加载 jquery-1.8.2.js.

目前我有:

<前>import jquery = module('jquery')

这导致名称jquery"在当前范围内不存在.

解决方案

FOR TYPESCRIPT 1.7+

看起来标准再次发生变化,低于 0.9+ 的方法仍然有效,但是随着 ES6 的到来,可以使用以下模块加载.(参考:https://github.com/TypeStrong/atom-typescript/问题/237#issuecomment-90372105)

import * as $ from "jquery";

甚至部分的

import {extend} from "jquery";

(这仍然需要 jquery.d.ts,如果安装了 tsd - tsd install jquery)

安装tsd:npm install tsd -g

对于 TYPESCRIPT 0.9+

///

此外,如果您的 jquery.d.ts 没有定义外部模块,请将以下内容添加到 jquery.d.ts:

声明模块jquery"{出口 = $;}

How do I require the jquery AMD module for my TypeScript module. For example let's say directory structure for scripts looks like this:


    jquery-1.8.2.js
    jquery.d.ts
    module.ts
    require.js

I want the generated js file from module.ts to require jquery-1.8.2.js be loaded via require.js.

Currently I have:


    import jquery = module('jquery') 

This results in The name "jquery" does not exist in the current scope.

解决方案

FOR TYPESCRIPT 1.7+

It looks like standard is changing again, where the below 0.9+ method still works, but with ES6 coming the following module loading could be used. (reference: https://github.com/TypeStrong/atom-typescript/issues/237#issuecomment-90372105)

import * as $ from "jquery";

and even partial ones

import {extend} from "jquery"; 

(this still require the jquery.d.ts, if tsd is installed - tsd install jquery)

to install tsd: npm install tsd -g

FOR TYPESCRIPT 0.9+

/// <reference path="../../typings/jquery/jquery.d.ts" />
import $ = require('jquery');

//Do your stuff

And also, if your jquery.d.ts do not define a external module, add the following to jquery.d.ts:

declare module "jquery" {
    export = $;
}

这篇关于如何在 TypeScript 中通过 AMD 要求 jquery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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