本文介绍了这些ES6导入方法有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这些导入方法有什么区别?

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导入方法有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 05:34