在ES6模块中导出多个类

amb*_*ent 114 javascript module export babel ecmascript-6

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

my/
??? module/
    ??? Foo.js
    ??? Bar.js
    ??? index.js
Run Code Online (Sandbox Code Playgroud)

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

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

// Bar.js
export default class Bar {
  // class definition
}
Run Code Online (Sandbox Code Playgroud)

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

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

export default {
  Foo,
  Bar,
}
Run Code Online (Sandbox Code Playgroud)

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

import {Foo, Bar} from 'my/module';
Run Code Online (Sandbox Code Playgroud)

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

web*_*deb 206

在您的代码中尝试此操作:

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

// without default
export {
  Foo,
  Bar,
}
Run Code Online (Sandbox Code Playgroud)

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

// 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'
Run Code Online (Sandbox Code Playgroud)

运用 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';
Run Code Online (Sandbox Code Playgroud)

与众不同 export default

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

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

// import it
import User from './user'
Run Code Online (Sandbox Code Playgroud)


Sye*_*yed 16

希望这可以帮助:

// 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();
Run Code Online (Sandbox Code Playgroud)


ino*_*tia 6

@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'
Run Code Online (Sandbox Code Playgroud)


小智 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';
Run Code Online (Sandbox Code Playgroud)


xav*_* bs 5

对于classes同一js文件中的多个文件,Component从扩展@wordpress/element,您可以这样做:

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

const Class1 = class extends Component {
}

const Class2 = class extends Component {
}

export { Class1, Class2 }
Run Code Online (Sandbox Code Playgroud)

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

import { Class1, Class2 } from './classes';
Run Code Online (Sandbox Code Playgroud)