问题
在我公司的 React Native 项目中,我遇到了这样的代码,聚合了来自多个模块的导出:
export Icon from "./Icon";
export Input from "./Input";
export Pills from "./Pills";
我的编辑强调这是无效语法。我检查了 MDN docs on export
事实上,他们没有列出这样的聚合语法。从他们列出的内容看来,合适的应该是:
export { default as Icon } from "./Icon";
export { default as Input } from "./Input";
export { default as Pills } from "./Pills";
但是第一个代码在我所在的 React Native 项目中工作。
问题
这是 JavaScript 中的有效语法,还是仅由我们在项目中使用的某些第三方包提供?
最佳答案
你是对的,但你不是真的使用 ES6 模块。
您正在使用 babel,带有 metro-react-native-babel-preset .这使得上述语法有效。
一如既往,有一个proposal使其成为语言的一部分,但目前处于第一阶段。
来自提案:
The proposed addition follows this same symmetric pattern:
Importing the "default" name (existing):
import v from "mod";
Exporting that name (existing):
import v from "mod"; export {v};
Symmetric "export from" (proposed):
export v from "mod";
关于javascript - `export Module from "module"` 是有效的 JavaScript 语法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57580725/