Export multiple classes in ES6 modules

前端 未结 6 825
鱼传尺愫
鱼传尺愫 2020-11-28 18:42

I\'m trying to create a module that exports multiple ES6 classes. Let\'s say I have the following directory structure:

my/
└── module/
    ├── Foo.js
    ├──         


        
相关标签:
6条回答
  • 2020-11-28 18:44

    Hope this helps:

    // Export (file name: my-functions.js)
    export const MyFunction1 = () => {}
    export const MyFunction2 = () => {}
    export const MyFunction3 = () => {}
    
    // if using `eslint` (airbnb) then you will see warning, so do this:
    const MyFunction1 = () => {}
    const MyFunction2 = () => {}
    const MyFunction3 = () => {}
    
    export {MyFunction1, MyFunction2, MyFunction3};
    
    // Import
    import * as myFns from "./my-functions";
    
    myFns.MyFunction1();
    myFns.MyFunction2();
    myFns.MyFunction3();
    
    
    // OR Import it as Destructured
    import { MyFunction1, MyFunction2, MyFunction3 } from "./my-functions";
    
    // AND you can use it like below with brackets (Parentheses) if it's a function 
    // AND without brackets if it's not function (eg. variables, Objects or Arrays)  
    MyFunction1();
    MyFunction2();
    
    0 讨论(0)
  • 2020-11-28 18:53

    @webdeb's answer didn't work for me, I hit an unexpected token error when compiling ES6 with Babel, doing named default exports.

    This worked for me, however:

    // Foo.js
    export default Foo
    ...
    
    // bundle.js
    export { default as Foo } from './Foo'
    export { default as Bar } from './Bar'
    ...
    
    // and import somewhere..
    import { Foo, Bar } from './bundle'
    
    0 讨论(0)
  • 2020-11-28 19:01

    For multiple classes in the same js file, extending Component from @wordpress/element, you can do that :

    // classes.js
    import { Component } from '@wordpress/element';
    
    const Class1 = class extends Component {
    }
    
    const Class2 = class extends Component {
    }
    
    export { Class1, Class2 }
    

    And import them in another js file :

    import { Class1, Class2 } from './classes';
    
    0 讨论(0)
  • 2020-11-28 19:01

    For exporting the instances of the classes you can use this syntax:

    // export index.js
    const Foo = require('./my/module/foo');
    const Bar = require('./my/module/bar');
    
    module.exports = {
        Foo : new Foo(),
        Bar : new Bar()
    };
    
    // import and run method
    const {Foo,Bar} = require('module_name');
    Foo.test();
    
    0 讨论(0)
  • 2020-11-28 19:06

    Try this in your code:

    import Foo from './Foo';
    import Bar from './Bar';
    
    // without default
    export {
      Foo,
      Bar,
    }
    

    Btw, you can also do it this way:

    // bundle.js
    export { default as Foo } from './Foo'
    export { default as Bar } from './Bar'
    export { default } from './Baz'
    
    // and import somewhere..
    import Baz, { Foo, Bar } from './bundle'
    

    Using export

    export const MyFunction = () => {}
    export const MyFunction2 = () => {}
    
    const Var = 1;
    const Var2 = 2;
    
    export {
       Var,
       Var2,
    }
    
    
    // Then import it this way
    import {
      MyFunction,
      MyFunction2,
      Var,
      Var2,
    } from './foo-bar-baz';
    

    The difference with export default is that you can export something, and apply the name where you import it:

    // export default
    export default class UserClass {
      constructor() {}
    };
    
    // import it
    import User from './user'
    
    0 讨论(0)
  • 2020-11-28 19:08
    // export in index.js
    export { default as Foo } from './Foo';
    export { default as Bar } from './Bar';
    
    // then import both
    import { Foo, Bar } from 'my/module';
    
    0 讨论(0)
提交回复
热议问题