在ES6模块中导出多个类

时间:2020-12-04 12:36:49

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

我正在尝试创建一个导出多个ES6类的模块。假设我有以下目录结构:

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

Foo.js and Bar.js each export a default ES6 class:

Foo.js和Bar.js都导出默认的ES6类:

// Foo.js
export default class Foo {
  // class definition
}

// Bar.js
export default class Bar {
  // class definition
}

I currently have my index.js set up like this:

我目前的index.js设置如下:

import Foo from './Foo';
import Bar from './Bar';

export default {
  Foo,
  Bar,
}

However, I am unable to import. I want to be able to do this, but the classes aren't found:

但是,我无法导入。我希望能够这样做,但找不到类:

import {Foo, Bar} from 'my/module';

What is the correct way to export multiple classes in an ES6 module?

在ES6模块中导出多个类的正确方法是什么?

4 个解决方案

#1


109  

Try this in your code:

在你的代码中尝试这个:

import Foo from './Foo';
import Bar from './Bar';

export { // without default
  Foo,
  Bar,
}

Btw, you can also do it this way:

顺便说一下,你也可以这样做:

//bundle.js
export Foo from './Foo'
export Bar from './Bar'

//and import somewhere..
import { 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 to export default

导出默认值的差异

is that you can export something, and apply the name where you import it

是你可以导出的东西,并在你导入它的地方应用名称

// export default
const Func = () {}
export default Func;

// import it
import Foo from './func'

#2


10  

Hope this helps:

希望这可以帮助:

// Export (file name: my-functions.js)
export const MyFunction1 = () => {}
export const MyFunction2 = () => {}
export const MyFunction3 = () => {}

// Import
import * as myFns from "./my-functions";

myFns.MyFunction1();
myFns.MyFunction2();
myFns.MyFunction3();


// OR Import it as Destructured
import { MyFunction1, MyFunction2 } 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();

#3


1  

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

@ webdeb的回答对我不起作用,我在用Babel编译ES6时遇到意外的令牌错误,做了命名的默认导出。

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'

#4


0  

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();

#1


109  

Try this in your code:

在你的代码中尝试这个:

import Foo from './Foo';
import Bar from './Bar';

export { // without default
  Foo,
  Bar,
}

Btw, you can also do it this way:

顺便说一下,你也可以这样做:

//bundle.js
export Foo from './Foo'
export Bar from './Bar'

//and import somewhere..
import { 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 to export default

导出默认值的差异

is that you can export something, and apply the name where you import it

是你可以导出的东西,并在你导入它的地方应用名称

// export default
const Func = () {}
export default Func;

// import it
import Foo from './func'

#2


10  

Hope this helps:

希望这可以帮助:

// Export (file name: my-functions.js)
export const MyFunction1 = () => {}
export const MyFunction2 = () => {}
export const MyFunction3 = () => {}

// Import
import * as myFns from "./my-functions";

myFns.MyFunction1();
myFns.MyFunction2();
myFns.MyFunction3();


// OR Import it as Destructured
import { MyFunction1, MyFunction2 } 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();

#3


1  

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

@ webdeb的回答对我不起作用,我在用Babel编译ES6时遇到意外的令牌错误,做了命名的默认导出。

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'

#4


0  

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();