【发布时间】:2013-01-09 14:43:06
【问题描述】:
我在TypeScript 中创建了一个项目。我使用RequireJS。一切正常,直到我需要 JQuery...
这是我的文件结构:
如您所见,我在modules 文件夹中有jquery-1.8.d.ts 文件。 jQuery 的 .js 文件位于 lib 文件夹中。当我使用 RequireJS 的配置文件导入模块时:
/// <reference path="../modules/require.d.ts" />
/// <reference path="AppMain.ts" />
require.config({
baseUrl: '../',
paths: {
jquery: 'lib/jquery-1.8.3'
},
shim: {
jquery: {
exports: '$'
}
},
name: "app/AppMain",
out: "../!built/Test.js",
optimize: "none"
});
require(['jquery', 'app/AppMain',
],
($, main) => {
var appMain = new main.AppMain();
appMain.run();
});
...然后将其导入JavaScript。如果我想把它放在TypeScript 里面怎么办?我需要使用JQuery、JQueryStatic 和JQueryPromise,在.d.ts 文件中声明。
当我尝试简单地导入一个新模块时,我得到一个错误:
import JQueryExternal = module("../../../modules/jquery-1.8.d"); // error: The name does not exist in the currect scope. A module cannot be aliased to a non-module type
interface Test {
MyMethod(): JQueryPromise; // here's a sample usage
}
一开始我以为路径不对,但我尝试将其移到classes 文件夹中,然后以其他类的方式导入它。同样的错误:/
如何以我想要的方式在TypeScript 中使用jQuery?有可能吗?
【问题讨论】:
标签: javascript requirejs typescript