ES6, how can you export an imported module in a single line?

前端 未结 6 590
梦毁少年i
梦毁少年i 2020-11-29 21:42

I\'d like to the following but with a single line, if possible:

  • import Module from \'./Module/Module;\'
  • export Module;
  • <
相关标签:
6条回答
  • 2020-11-29 21:49

    I don't know why but just this works for me :

    components/index.js:

    import Component from './Component';
    import Component2 from './Component2';
    import Component3 from './Component3';
    import Component4 from './Component4';
    
    export {Component, Component2, Component3, Component4};
    

    I import the exports like this :

    import {Component, Component2, Component3, Component4} from '../components';
    
    0 讨论(0)
  • 2020-11-29 21:53

    For React Native components this syntax works for me:

    export {default} from 'react-native-swiper';
    
    0 讨论(0)
  • 2020-11-29 21:56

    Please note you can also re-export everything from a module:

    export * from './Module/Module';
    
    0 讨论(0)
  • 2020-11-29 21:56

    So, I've found this to work quite well for the immediate export functionality of having an index.js at the root of the components directory for easy referencing:

    import Component from './Component/Component'
    import ComponentTwo from './ComponentTwo/ComponentTwo'
    
    module.exports = {
      Component,
      ComponentTwo
    };
    

    You need to use module.exports.

    0 讨论(0)
  • 2020-11-29 21:59
    // Service
    // ...
    export const paymentService = new PaymentService()
    
    // Entry services/index.js file
    export * from './paymentService'
    
    // use it like
    import { paymentService } from './services/'
    
    0 讨论(0)
  • 2020-11-29 22:02
    export {default as Module} from './Module/Module';
    

    is the standard ES6 way, as long as you don't need Module to also be available inside the module doing the exporting.

    export Module from './Module/Module';
    

    is a proposed ESnext way to do it, but that only works if you've enabled it in Babel for now.

    0 讨论(0)
提交回复
热议问题