导出 ES6 模块中的多个类
Export multiple classes in ES6 modules
我正在尝试创建一个导出多个 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 模块中导出多个类的正确方法是什么?
在你的代码中试试这个:
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 (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();
@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'
// 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';
对于同一js
文件中的多个classes
,从@wordpress/element
扩展Component
,您可以这样做:
// 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';
你可以做到。导出{类名、类名等}
要导出类的实例,可以使用以下语法:
// 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();
相关文章:
- 在一个表达式中加载并使用ES6模块
- ES6模块进口是否吊装
- 使用ES6模块创建JS
- 无法使用Webpack加载ES6模块
- 可以'I don’我不明白;使用webpack和es6模块进行EventEmitter
- 观察ES6模块属性
- NPM:取决于本地 ES6 模块(无转译)
- 使用 Babel 转译器导入类并调用带有 ES6 模块的静态方法
- 使用 Babel 将 ES6 模块转换为 ES5 AMD 模块,无法按预期工作
- ES6模块导入和依赖关系管理
- browserify/requirejs模块和ES6模块有什么区别
- 使用es6模块:未捕获ReferenceError:define未定义
- 如何通过名称(从模块本身)访问ES6模块变量
- 如何有条件地导入 ES6 模块
- 使用webpack和babel加载程序导入导出ES6模块
- ES6模块初始化顺序错误
- ES6模块:为什么先前导出的模块不为“;“孩子”;模块
- 流星 ES6 模块相对路径
- ES6 模块不适用于 babel 6 和 gulp
- 使用 webpack 和 ts-loader 重新导出 ES6 模块