模块加载如何与 TypeScript 配合使用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12681155/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 16:10:23  来源:igfitidea点击:

How Does Module Loading Work with TypeScript

typescript

提问by EisenbergEffect

In TypeScript, if I am targeting a browser, how does module loading work? Can I use require.js to load modules? does it have it's own loader?

在 TypeScript 中,如果我的目标是浏览器,模块加载是如何工作的?我可以使用 require.js 加载模块吗?它有自己的装载机吗?

回答by chuckj

TypeScript does not provide a runtime. You need to supply a module loader to use, such as requirejs. A TypeScript module can either be generated to CommonJS convention (for use with node.js) or AMD convention (as used in requirejs); which it generates is a compiler switch.

TypeScript 不提供运行时。你需要提供一个模块加载器来使用,比如 requirejs。TypeScript 模块可以生成为 CommonJS 约定(用于 node.js)或 AMD 约定(如 requirejs 中使用);它生成的是一个编译器开关。

回答by dade

As Chuckj mentioned, TypeScript does not provide a runtime. You need to supply a module loader to use.

正如 Chuckj 所提到的,TypeScript 不提供运行时。您需要提供要使用的模块加载器。

What you then need to do is to tell the TypeScript compiler to generate the JS to confirm with the module loader that would be used at runtime.

然后你需要做的是告诉 TypeScript 编译器生成 JS 以确认将在运行时使用的模块加载器。

You can do this by specifying the module loader to the compiler using -m compiler flag:

您可以通过使用 -m 编译器标志将模块加载器指定给编译器来完成此操作:

tsc -m commonjs //'amd', 'system', 'umd' or 'es2015'

or by specifying the module in the compilerOptionsin your tsconfig.jsonfile:

或者通过compilerOptions在您的tsconfig.json文件中指定模块:

{
    "compilerOptions": {
        "noImplicitAny": true,
        "module": "commonjs" //'amd', 'system', 'umd' or 'es2015'
    },
    "exclude": [
        "node_modules"
    ]
}