在 ES 6 模块中重新导出默认值 [英] Re-export default in ES 6 modules
问题描述
在 ES6 中,是否可以缩短以下代码.我有一个 App.js
文件和一个 index.js
.
In ES6, is it possible to shorten the following code. I have an App.js
file and an index.js
.
index.js
import App from './App';
export default App;
类似的东西
index.js
export default App from './App.js'
推荐答案
如果你使用 proposal-export-default-from
Babel 插件(它是 stage-1
preset),您将能够使用以下代码重新导出默认值:
If you use proposal-export-default-from
Babel plugin (which is a part of stage-1
preset), you'll be able to re-export default using the following code:
export default from "./App.js"
有关更多信息,请参阅ECMAScript 提案.
For more information see the ECMAScript proposal.
另一种方式(没有这个插件)是:
Another way (without this plugin) is:
export { default as App } from "./App.js"
当单独的文件(每个文件都有自己的export
)具有所有共同点(例如,utils
)时,上述是一种非常常见的做法,因此如果,例如, 一个人会想要导入 3 个 utility 函数,而不必编写多个 imports:
The above is a very common practice when separate files, each with its own export
, have all something in common, for example, utils
, so if, for example, one would want to import 3 utility functions, instead of having to write multiple imports:
import util_a from 'utils/util_a'
import util_b from 'utils/util_b'
import util_c from 'utils/util_c'
可以在一行中导入任何实用程序:
One could import any of the utilities in a single-line:
import { util_a, util_b , util_c } from 'utils'
通过在 /utils
文件夹中创建一个 index.js
文件并导入那里所有实用程序的所有默认值并重新导出,因此 index
文件将作为网关"对于与该文件夹相关的所有导入.
By creating an index.js
file in the /utils
folder and import all the defaults of all the utilities there and re-export, so the index
file will serve as the "gateway" for all imports related to that folder.
这篇关于在 ES 6 模块中重新导出默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!