javascript导入中成员和默认成员之间有什么区别? [英] What's the difference between a member and a default member in javascript imports?

查看:119
本文介绍了javascript导入中成员和默认成员之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读javascript导入此处并且遇到了成员与默认成员的概念,但无法找到差异的解释。将类声明为 default 的好处是什么,而不是在导入时不必在括号中包含该名称?

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. What is the benefit of declaring a class as default other than not having to include the name in brackets when importing?

推荐答案

因为还有 2种从模块导出内容的方法

常规 export 每个成员的出口

// module.js
export foo
export bar

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

export default 定义默认导出。

// module.js
export default Foo

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

至于优点,它取决于用例。某些库基于每个成员导出API,您可以单独导入它们。其他人将一个对象中的所有内容导出为默认导出。像Rollup这样的代码捆绑商使用每个成员的进口作为在捆绑过程中树木化不需要的进口的方法。

As for advantages, it depends on the use case. Some libraries export APIs on a per member basis, which you can import individually. 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.

这篇关于javascript导入中成员和默认成员之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆