typescript 打字稿中的导入与需要
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39658232/
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
Import vs Require in Typescript
提问by refactor
While I was going through Angular2 documentation, I came across below code in here.
当我浏览 Angular2 文档时,我在这里遇到了下面的代码。
src/polyfills.ts
src/polyfills.ts
import 'core-js/es6';
import 'core-js/es7/reflect';
require('zone.js/dist/zone');
if (process.env.ENV === 'production') {
// Production
} else {
// Development
Error['stackTraceLimit'] = Infinity;
require('zone.js/dist/long-stack-trace-zone');
}
In the above code we can see that there are both import
and require
statements.
在上面的代码中,我们可以看到有import
andrequire
语句。
"core-js" and "zone.js" are both node modules.
“core-js”和“zone.js”都是节点模块。
My question is; why is import
used for core-js and require
for "zone.js", is there any specific reason for this?
我的问题是;为什么import
用于 core-js 和require
“zone.js”,这有什么具体原因吗?
回答by James Monger
With TypeScript, import
can be used if there is a declaration file (see Declaration Files in basarat's book) for the module.
import
如果有模块的声明文件(请参阅basarat 书中的声明文件),则可以使用TypeScript 。
If there isn't a declaration file, the TypeScript compiler doesn't know if the module exists, so you need to use require
instead which lacks the compilation checking.
如果没有声明文件,TypeScript 编译器不知道该模块是否存在,因此您需要使用require
缺少编译检查的代替。