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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 03:52:05  来源:igfitidea点击:

Import vs Require in Typescript

node.jstypescript

提问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 importand requirestatements.

在上面的代码中,我们可以看到有importandrequire语句。

"core-js" and "zone.js" are both node modules.

“core-js”和“zone.js”都是节点模块。

My question is; why is importused for core-js and requirefor "zone.js", is there any specific reason for this?

我的问题是;为什么import用于 core-js 和require“zone.js”,这有什么具体原因吗?

回答by James Monger

With TypeScript, importcan 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 requireinstead which lacks the compilation checking.

如果没有声明文件,TypeScript 编译器不知道该模块是否存在,因此您需要使用require缺少编译检查的代替。