[英]ES6: “import * as alias” vs “import alias”
Is there any difference between:之间有什么区别:
import utils from 'utils'
and和
import * as utils from 'utils'
? import * as utils from 'utils'
?
In case A:情况 A:
//utils.js
export function doSomething()
{
//...
}
In case B:情况 B:
//utils.js
export function doSomething()
{
//...
}
export default function doSomethingDefault()
{
//...
}
UPDATE :更新:
I was mislead by intellisense feature of vscode, but as recommended a small test on node+babel showed the difference:我被 vscode 的智能感知功能误导了,但按照推荐,对 node+babel 的一个小测试显示了差异:
//index.js
import utilsCaseA from './utils1'
import * as utilsCaseAWildcard from './utils1'
var utilsCaseARequire = require('./utils1')
import utilsCaseB from './utils2'
import * as utilsCaseBWildcard from './utils2'
var utilsCaseBRequire = require('./utils2')
var compareObjects =
{
utilsCaseA, utilsCaseAWildcard, utilsCaseARequire,utilsCaseB,utilsCaseBWildcard,utilsCaseBRequire
};
console.log(compareObjects);
From your example:从你的例子:
Case A:案例一:
//utils.js
export function doSomething()
{
//...
}
Case B:案例B:
//utils.js
export function doSomething()
{
//...
}
export default function doSomethingDefault()
{
//...
}
Result:结果:
import utils from 'utils'
utils() // (Case A: undefined, Case B: doSomethingDefault)
import * as utils from 'utils'
utils // (Case A: utils = { doSomething: function }, Case B: utils = { doSomething: function, default: function })
import { doSomething } from 'utils'
doSomething() // (both Case A and Case B: doSomething = doSomething)
The difference between Case A and Case B is that, in Case A import utils from 'utils'
, utils
will be undefined
because there is no default export.案例 A 和案例 B 之间的区别在于,在案例 A
import utils from 'utils'
, utils
将是undefined
因为没有默认导出。 In case B, utils
will refer to doSomethingDefault
.在情况 B 中,
utils
将引用doSomethingDefault
。
With import * as utils from 'utils'
, in Case A utils
will have one method ( doSomething
), while in Case B utils
will have two methods ( doSomething
and default
).使用
import * as utils from 'utils'
,在 Case A utils
将有一个方法( doSomething
),而在 Case B utils
将有两个方法( doSomething
和default
)。
import utils from 'utils'
imports default from 'utils' package. import utils from 'utils'
从 'utils' 包中导入默认值。 undefined
in the case provided.在提供的情况下
undefined
。
import * as utils from 'utils'
imports entire module exports
object with all available named exports including default. import * as utils from 'utils'
导入整个模块exports
对象以及所有可用的命名导出,包括默认值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.