Export multiple classes in ES6 modules

前端 未结 6 824
鱼传尺愫
鱼传尺愫 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 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();
    

提交回复
热议问题