简体   繁体   English

javascript导入中的成员和默认成员之间有什么区别?

[英]What's the difference between a member and a default member in javascript imports?

I was reading about javascript imports here and came across the concept of members vs default members but couldn't find an explanation of what the difference is. 我在这里阅读了有关javascript导入的信息,并遇到了成员与默认成员的概念,但是找不到关于两者之间的区别的解释。 What is the benefit of declaring a class as default other than not having to include the name in brackets when importing? 除了在导入时不必在括号中包含名称之外,将一个类声明为default类还有什么好处?

Because there's also 2 ways to export stuff from a module : 因为还有两种从模块中导出内容的方法

regular export which exports per member 定期export ,按会员出口

// module.js
export foo
export bar

// The dependent
import {foo, bar} from './module';

and export default which defines the default export. export default ,定义默认导出。

// module.js
export default Foo

// The dependent
import Foo from './module';

As for advantages, it depends on the use case. 至于优点,这取决于用例。 Some libraries export APIs on a per member basis, which you can import individually. 有些库会按成员导出API,您可以分别导入。 Others export everything in one object as the default export. 其他人将所有内容导出为一个对象作为默认导出。 Code bundlers like Rollup use per-member imports as a way to tree-shake unneeded imports during bundling as well. 诸如Rollup之类的代码捆绑器也使用按成员导入,从而在捆绑过程中摇晃不需要的导入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM