Re-export default in ES 6 modules

早过忘川 提交于 2020-01-30 15:20:08

问题


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;

Something like this

index.js

export default App from './App.js'

回答1:


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"

For more information see the ECMAScript proposal.


Another way (without this plugin) is:

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



回答2:


import App from './App';

export default App;

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

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

Related discussions:

  • TC39 proposal: https://github.com/tc39/proposal-export-default-from#common-concerns



回答3:


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

1. default export

(This appears to be what OP wants)

export { default } from './App'

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

2. named export

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

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

These will work with react as vsync's answer states.



来源:https://stackoverflow.com/questions/39999282/re-export-default-in-es-6-modules

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!