Javascript 在 ES 6 模块中重新导出默认值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39999282/
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 23:08:32  来源:igfitidea点击:

Re-export default in ES 6 modules

javascriptecmascript-6babeljses6-modules

提问by sanchit

In ES6, is it possible to shorten the following code. I have an App.jsfile and an index.js.

在 ES6 中,是否可以缩短以下代码。我有一个App.js文件和一个index.js.

index.js

索引.js

import App from './App';

export default App;

Something like this

像这样的东西

index.js

索引.js

export default App from './App.js'

回答by Micha? Per?akowski

If you use proposal-export-default-fromBabel plugin(which is a part of stage-1preset), you'll be able to re-export default using the following code:

如果您使用proposal-export-default-fromBabel 插件(这是stage-1preset的一部分),您将能够使用以下代码重新导出默认值:

export default from "./App.js"

For more information see the ECMAScript proposal.

有关更多信息,请参阅ECMAScript 提案



Another way (without this plugin) is:

另一种方式(没有这个插件)是:

export { default } from "./App.js"

回答by vsync

import App from './App';

export default App;

?

?

Babel 7 (with @babel/preset-react) can transform the below:

Babel 7 (with @babel/preset-react) 可以转换以下内容:

export { default as App } from './App.js';


Related discussions:

相关讨论:

回答by Fernando Rojo

This is a bit of repetition from the previous answers, but to clarify the difference in two options:

这是对先前答案的一些重复,但要澄清两个选项的区别:

1. default export

1.默认导出

(This appears to be what OP wants)

(这似乎是 OP 想要的)

export { default } from './App'

// in a different file
import App from './index'

2. named export

2.命名导出

export { default as App } from './App'

// in another file
import { App } from './index'

These will work with reactas vsync's answerstates.

这些将与reactvsync 的回答状态一起使用。