在ES6模块中导出多个类


150

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

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

Foo.js并且Bar.js每个导出默认的ES6类:

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

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

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

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

export default {
  Foo,
  Bar,
}

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

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

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


5
export不用默认即可使用
webdeb

您只能进行一次default导出。想象如果有人尝试这样做import SomeClass from 'my/module'。这将自动default从该路径导入模块。如果您在那里有多个默认导出,它将如何知道要导入哪个?
saadq '16

Answers:


258

在您的代码中尝试以下操作:

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

// without default
export {
  Foo,
  Bar,
}

顺便说一句,您也可以这样操作:

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

使用 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';

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

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

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

Unexpected token在建造时遇到错误export Foo from './Foo'; export Bar from './Bar'
inostia

@inostia注意,这是ES6语法,您可能需要“ babel”才能支持它
webdeb

我正在使用通天塔。使用webpack编译时出现该错误。我想我需要做些类似的事情export { default as Foo } from './Foo';。我在其他地方看到过
inostia

@inostia我也遇到了这个问题,export { default as Foo } from './Foo';实际上需要将其导出。
echolocation '18

17

希望这可以帮助:

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

7

@webdeb的答案对我不起作用,unexpected token使用Babel编译ES6并执行命名默认导出时遇到错误。

这对我有用,但是:

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

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

-2

要导出类的实例,可以使用以下语法:

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

4
这不是ES6语法
GerDner
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.