TypeScript 使用所需的定义编译 AMD 模块
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12706167/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
TypeScript compile AMD modules with required defines
提问by Peter StJ
In AMD (as implemented in requirejs) one can defined modules to be included as dependencies, eg:
在 AMD(在 requirejs 中实现)中,可以定义要作为依赖项包含的模块,例如:
define(['require','exports'], function(require, exports) {
var externalDep = require('path/to/depModule');
// Use the module somewhere.
});
I have tried the --module amd and it outputs correctly an AMD module usable by requirejs.
我已经尝试过 --module amd 并且它正确输出了一个可由 requirejs 使用的 AMD 模块。
Is it possible to define dependencies inside the source of TypeScript source file that translates to something like the example above?
是否可以在 TypeScript 源文件的源中定义依赖项,转换为类似于上面的示例?
回答by mohamed hegazy
You need to "export" your modules;
您需要“导出”您的模块;
export module depModule {
export class A {
}
}
that will transalate into JavaScript code that looks like:
这将转换为如下所示的 JavaScript 代码:
define(["require", "exports"], function(require, exports) {
(function (depModule) {
var A = (function () {
function A() { }
return A;
})();
depModule.A = A;
})(exports.depModule || (exports.depModule = {}));
})
and then you consume them by using "import":
然后你通过使用“导入”来消耗它们:
module otherModule {
import depModule = module('depModule');
var a = new depModule.depModule.A();
}
you will need to specify the type of your module code generation to the compiler using --module AMD.
您需要使用 --module AMD 向编译器指定模块代码生成的类型。

