Exportar múltiples clases en módulos ES6
Estoy intentando crear un módulo que exporte múltiples clases ES6. Digamos que tengo la siguiente estructura de directorios:
my/
└── module/
├── Foo.js
├── Bar.js
└── index.js
Foo.js
y Bar.js
cada uno exporta una clase ES6 predeterminada:
// Foo.js
export default class Foo {
// class definition
}
// Bar.js
export default class Bar {
// class definition
}
Actualmente tengo mi index.js
configurado de esta manera:
import Foo from './Foo';
import Bar from './Bar';
export default {
Foo,
Bar,
}
Sin embargo, no puedo importar. Quiero ser capaz de hacer esto, pero las clases no se encuentran:
import {Foo, Bar} from 'my/module';
¿Cuál es la forma correcta de exportar varias clases en un módulo ES6?
4 answers
Prueba esto en tu código:
import Foo from './Foo';
import Bar from './Bar';
export { // without default
Foo,
Bar,
}
Por cierto, también puedes hacerlo de esta manera:
//bundle.js
export Foo from './Foo'
export Bar from './Bar'
//and import somewhere..
import { Foo, Bar } from './bundle'
Usando 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';
La diferencia con export default
Es que puedes exportar algo, y aplicar el nombre donde lo importas
// export default
const Func = () {}
export default Func;
// import it
import Foo from './func'
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-08-10 20:05:31
Espero que esto ayude:
// 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();
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-06-07 09:32:00
La respuesta de@webdeb no funcionó para mí, me encontré con un error unexpected token
al compilar ES6 con Babel, haciendo exportaciones predeterminadas con nombre.
Esto funcionó para mí, sin embargo:
// 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'
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-01-22 22:06:04
Para exportar las instancias de las clases puede usar esta sintaxis:
// 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();
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-05-05 20:33:11