Javascript ES6`从导入导出*?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38077164/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 21:03:10  来源:igfitidea点击:

ES6 `export * from import`?

javascriptecmascript-6babeljs

提问by Jordan Feldstein

Is there a syntax using ES6 or ES7 or babel which will allow me to easily bundle together many groups of sub files?

是否有使用 ES6 或 ES7 或 babel 的语法可以让我轻松地将多组子文件捆绑在一起?

E.g., given:

例如,给定:

./action_creators/index.js
./action_creators/foo_actions.js
./action_creators/bar_actions.js

Have index.js import foo and bar actions, then re-export them, so I can

让 index.js 导入 foo 和 bar 动作,然后重新导出它们,这样我就可以

import {FooAction, BarAction} from './action_creators/index.js'

I don't want to have to remember / change references if I were to change which file I've organized the objects themselves into.

如果我要更改将对象本身组织到哪个文件中,我不想必须记住/更改引用。

回答by Timo

Yes, ES6 supports directly exporting imported modules:

是的,ES6 支持直接导出导入的模块

export { name1, name2, …, nameN } from …;

export {FooAction, BarAction} from './action_creators/index.js'


You can also re-export all exportsof the imported module using the *syntax:

您还可以使用以下语法重新导出导入模块的所有导出*

export * from …;

export * from './action_creators/index.js';


More info on MDN.

有关 MDN 的更多信息。

回答by Gerson Diniz

Defaultexport as Default:

默认导出为Default

export {default} from './something';

Defaultexport as Named:

默认导出为Named

export {default as foo} from './something';

Namedexport as Default:

命名导出为默认

export {foo as default} from './something';

Namedexport as Named:

命名导出为Named

export {foo} from './something';