ES6模块中如何导出多个类

217

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

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

Foo.jsBar.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模块中正确导出多个类的方式是什么?


8
只需使用 export 而不需要使用默认值。 - webdeb
3
一个模块只能有一个 default 导出。想象一下,如果有人尝试执行 import SomeClass from 'my/module',它会自动从这个路径导入 default 模块。如果在那里有多个默认导出,它怎么知道要导入哪一个呢? - Saad
7个回答

374

在你的代码中尝试这个:

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'

在构建export Foo from './Foo'; export Bar from './Bar'时,我遇到了一个“意料之外的标记”错误。 - inostia
请注意,这是ES6语法,您可能需要使用"babel"来支持它。 - webdeb
我正在使用 Babel。在使用 webpack 进行编译时,我遇到了这个错误。我认为我需要做类似于 export { default as Foo } from './Foo'; 的操作。我在其他地方也看到过这种写法。 - inostia
@inostia 我也遇到了这个问题,需要使用 export { default as Foo } from './Foo'; 来实际导出它。 - echolocation

26

希望这可以帮到你:

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

9

@webdeb的答案对我不起作用,我在使用Babel编译ES6时遇到了"unexpected token"错误,这是由于命名默认导出导致的。

然而,以下方法对我有效:

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

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

5

如果在同一个 js 文件中有多个 ,并且想要扩展自@wordpress/elementComponent,则可以这样做:

// classes.js
import { Component } from '@wordpress/element';

const Class1 = class extends Component {
}

const Class2 = class extends Component {
}

export { Class1, Class2 }

将它们导入到另一个 js 文件中:

import { Class1, Class2 } from './classes';

0

你可以这样做:export {类名, 类名等等}


-5

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

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

6
这不是ES6语法。 - GerDner

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接