Javascript 在 ES6 模块中导出多个类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38340500/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 21:21:37  来源:igfitidea点击:

Export multiple classes in ES6 modules

javascriptmoduleexportecmascript-6babel

提问by ambient

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.jsand Bar.jseach 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.jsset 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 模块中导出多个类的正确方法是什么?

回答by webdeb

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

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 defaultis that you can export something, and apply the name where you import it:

与 的区别export default在于您可以导出某些内容,并在导入时应用名称:

// export default
export default class UserClass {
  constructor() {}
};

// import it
import User from './user'

回答by Syed

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

回答by inostia

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

@webdeb 的回答对我不起作用,我unexpected token在使用 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'

回答by PaoPao

// 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';

回答by Schmidko

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