我有一个模块netmap
导出默认类NetMap
:
export default class NetMap {...}
我有另一个模块 helloworld
,我想导出(不是作为 默认
)整个 NetMap
类,以便另一个模块可以访问NetMap
使用:
import * as helloworld from 'helloworld'
const x = helloworld.NetMap()
这可能吗? NetMap
的 export
在 helloworld
模块中会是什么样子?
最佳答案
netmap.js
export default class NetMap {
...
}
helloworld.js
(通常称为 barrel ):
import NetMap from './netmap.js';
import Foo from '...';
import ...
export {
NetMap,
Foo,
...
};
然后,在另一个模块中:
import * as helloworld from './helloworld.js';
const x = new helloworld.NetMap();
但我个人更喜欢使用命名导入/导出,所以我会这样做:
netmap.js
export class NetMap {
...
}
helloworld.js
(通常称为 barrel ):
export { NetMap } from './netmap.js';
export { Foo } from '...';
export { ...
然后,在另一个模块中:
import * as helloworld from './helloworld.js';
const x = new helloworld.NetMap();
或者:
import { NetMap } from './helloworld.js';
const x = new NetMap();
关于javascript - 从另一个模块导出模块类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51033247/