ES6 module syntax: is it possible to `export * as Name from …`?

前端 未结 4 1706
小蘑菇
小蘑菇 2020-12-01 23:17

See question title. I found a great reference for the forms of export available, but I have not seen what I\'m looking for.

Is it possible to do somethi

相关标签:
4条回答
  • 2020-12-01 23:56

    The proposal for this spec has merged to ecma262. If you're looking for this functionality in an environment that is running a previous JS, there's a babel plugin for it! After configuring the plugin (or if you're using ecma262 or later), you are able to run the JS in your question:

    // file: constants.js
    export const SomeConstant1 = 'yay';
    export const SomeConstant2 = 'yayayaya';
    
    // file: index.js
    export * as Constants from './constants.js';
    
    // file: component.js
    import { Constants } from './index.js';
    
    const newVar = Constants.SomeConstant1; // 'yay'
    
    0 讨论(0)
  • 2020-12-02 00:06
    // file: index.js
    // note, this doesn't have to be at the top, you can put it wherever you prefer
    import * as AllExportsFromThisModule from "./index.js"; // point this at the SAME file
    export default AllExportsFromThisModule;
    
    export const SOME_CONSTANT = 'yay';
    export const SOME_OTHER_CONSTANT = 'yayayaya';
    
    0 讨论(0)
  • 2020-12-02 00:10

    Today in 2019, it is now possible.

    export * as name1 from …;
    
    0 讨论(0)
  • 2020-12-02 00:11

    No, it's not allowed in JS either, however there is a proposal to add it. For now, just use the two-step process with importing into a local variable and exporting that:

    // file: constants.js
    export const SomeConstant1 = 'yay';
    export const SomeConstant2 = 'yayayaya';
    
    // file: index.js
    import * as Constants from './constants.js';
    export {Constants};
    
    0 讨论(0)
提交回复
热议问题