问题描述
这些导入方法有什么区别?
What is the difference between these import methods?
方法1:
import {sum, pi} from "lib/math";
方法2:
import exp, {pi, e} from "lib/mathplusplus";
es2015文档显示了这两个示例,我无法弄清花括号的目的。似乎所有导入后列出的东西都将被分配给窗口
对象。
The es2015 docs showed these two examples, and I can't figure out the purpose of the curly braces. It seems like all of the things listed after import will be assigned to the window
object anyway.
文档供参考:
推荐答案
模块可以导出多个东西。模块也可以有一个默认导出。
modules can export multiple things. Modules can also have a single "default" export.
从somelib导入exp;
将 somelib
的默认导出分配给变量 exp
。
This assigns the default export of somelib
to the variable exp
.
从somelib导入{a,b};
这将非默认命名导出 a
和 b
分配给局部变量 a
和 b
。
This assigns the non-default named exports a
and b
to local variables a
and b
.
import exp,{a,b} fromsomelib;
将默认导出分配给 exp
和命名导出到
a
和 b
。
import * as somelib fromsomelib;
并将它们作为对象分配给局部变量 somelib
,这意味着您将具有 somelib.a
, somelib.b
等。
Takes all of the named exports of somelib and assigns them as an object to the local variable somelib
, which means you will have somelib.a
, somelib.b
, etc.
这是一个非常好的资源:
This is a very good resource for the topic: http://www.2ality.com/2014/09/es6-modules-final.html
这篇关于这些ES6导入方法有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!