How to export imported object in ES6?

后端 未结 5 465
孤城傲影
孤城傲影 2020-12-07 06:55

The use case is simple: I just want to export an object with the name just as it was imported.

for example:

import React from \'react\';
export React         


        
5条回答
  •  轮回少年
    2020-12-07 07:32

    Given ./foo.js:

    const Foo = class {
      talk() { return 'hello'; }
    };
    
    export default Foo;
    

    Then you should be able to do this:

    import Foo from './foo';
    
    let foo = new Foo();
    
    foo.talk(); // => 'hello';
    

    The syntax more or less follows the commonjs module.exports pattern, where you would do this:

    const Foo = class {
    
    };
    
    module.exports = Foo;
    

    More here:

    http://exploringjs.com/es6/ch_modules.html

提交回复
热议问题