为什么不应该在ES6中使用全部导入 [英] Why shouldn't you use import all in ES6
问题描述
因此,我最近开始学习反应,并注意到所有文档都具有与其他文件相似的导入内容:
So I recently started learning react and noticed that all the documentation has imports that look like else:
import { Apples, Bananas, Oranges } from 'fruits';
但是在研究反应时,我发现这种语法同样有效:
But while studying react I found that this syntax works just as well:
import * as Fruits from 'fruits';
我的问题:使用import all语法是否会导致性能下降或逻辑冲突?
My Question: Is there any performance loss or logical conflict with using the import all syntax?
如果没有,我将继续使用该语法.我宁愿稍微冗长一些,也不必担心确保已导入所有内容.
If there is not then I will just keep using that syntax. I would rather be a little more verbose and not have to worry about making sure everything has been imported.
推荐答案
实际上-这取决于给定模块的导出量.
Actually - it depends on the amount of exports from given module.
如果您导入例如Lodash
您可能不想导入整个库,应该只导入将在应用程序中使用的以下方法:
If you import e.g. Lodash
you might not want to import whole library, you should import only these methods which you are going to use in your application:
import { isEmpty, pickBy, orderBy } from 'lodash';
避免性能损失和内存浪费.
to avoid performance loss and memory waste.
但是,如果给定的模块仅包含几种方法,或者基本上您将要使用每个单个导出,那么您可以自由地使用该快捷方式:
However, if your given module holds just a few methods or basically you are going to use every single export, then you can freely use that shortcut:
import * as Fruits from 'fruits';
注意:我想您正在使用webpack 2
,它实际上包括三摇算法,可以最小化捆绑包.
Note: I suppose you are using webpack 2
, which actually includes three-shaking algorithm which makes minifying the bundle possible.
这篇关于为什么不应该在ES6中使用全部导入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!