导入http = require('http');并从'http'导入*作为http;?

时间:2021-07-29 13:29:25

I haven't been able to find a worthwhile NodeJS with Typescript tutorial out there so I'm diving in unguided and sure enough I have a question.

我无法在那里找到一个带有Typescript教程的有价值的NodeJS,所以我在没有指导的情况下潜水,当然我有一个问题。

I don't understand the difference between these two lines:

我不明白这两行之间的区别:

import * as http from 'http';
// and
import http = require('http');

They seem to function the same way but I imagine there's probably some nuance to their behavior or else one of them probably wouldn't exist.

他们似乎以相同的方式运作,但我想他们的行为可能有一些细微差别,否则他们中的一个可能就不存在了。

I do understand that the first approach could let me selectively import from a module but if I'm importing all of the module then is there a difference between the two? Is there a preferred way? What if I'm importing from my own files, does that change anything?

我明白第一种方法可以让我有选择地从一个模块导入,但如果我导入所有的模块,那么两者之间是否存在差异?有首选方式吗?如果我从我自己的文件导入,这会改变什么呢?

2 个解决方案

#1


4  

In the first form, you create an http object in your code (totally clean), then, the interpreter will look for each possible import in http module and append it, one by one, to the http object in your code, this is a little slower (not much) than the second form where you are getting the module.exports object defined in the http module, then copying this reference to a new http object in your code, this is object in a node special function with a particular context, not only an object created in your code with the contents of the module.

在第一种形式中,您在代码中创建一个http对象(完全干净),然后,解释器将在http模块中查找每个可能的导入并将其逐个附加到代码中的http对象,这是一个你得到http模块中定义的module.exports对象,然后将此引用复制到代码中的新http对象,这是具有特定上下文的节点特殊函数中的对象,比第二种形式慢一点(不多) ,不仅是在代码中使用模块内容创建的对象。

#2


2  

While in a node environment where you've configured the module type to be common JS the output will be the same. Other module frameworks will utilize different syntax and by using the first approach you have the flexibility to change that at will.

在节点环境中,您已将模块类型配置为通用JS,输出将是相同的。其他模块框架将使用不同的语法,并且通过使用第一种方法,您可以灵活地随意更改它。

Also of note about the import * as http from 'http'; approach is that it is the ES6 module import syntax, so once you are in an environment that fully supports ES6 your imports will just work.

还有关于导入*作为来自'http'的http的注释;方法是它是ES6模块的导入语法,所以一旦你在一个完全支持ES6的环境中,你的导入就可以了。

#1


4  

In the first form, you create an http object in your code (totally clean), then, the interpreter will look for each possible import in http module and append it, one by one, to the http object in your code, this is a little slower (not much) than the second form where you are getting the module.exports object defined in the http module, then copying this reference to a new http object in your code, this is object in a node special function with a particular context, not only an object created in your code with the contents of the module.

在第一种形式中,您在代码中创建一个http对象(完全干净),然后,解释器将在http模块中查找每个可能的导入并将其逐个附加到代码中的http对象,这是一个你得到http模块中定义的module.exports对象,然后将此引用复制到代码中的新http对象,这是具有特定上下文的节点特殊函数中的对象,比第二种形式慢一点(不多) ,不仅是在代码中使用模块内容创建的对象。

#2


2  

While in a node environment where you've configured the module type to be common JS the output will be the same. Other module frameworks will utilize different syntax and by using the first approach you have the flexibility to change that at will.

在节点环境中,您已将模块类型配置为通用JS,输出将是相同的。其他模块框架将使用不同的语法,并且通过使用第一种方法,您可以灵活地随意更改它。

Also of note about the import * as http from 'http'; approach is that it is the ES6 module import syntax, so once you are in an environment that fully supports ES6 your imports will just work.

还有关于导入*作为来自'http'的http的注释;方法是它是ES6模块的导入语法,所以一旦你在一个完全支持ES6的环境中,你的导入就可以了。