[英]Export multiple classes in ES6 modules
我正在嘗試創建一個導出多個 ES6 類的模塊。 假設我有以下目錄結構:
my/
└── module/
├── Foo.js
├── Bar.js
└── index.js
Foo.js
和Bar.js
分別導出一個默認的 ES6 class:
// 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';
你可以做。 導出{className、className 等}
要導出類的實例,您可以使用以下語法:
// 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();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.