javascript - 从另一个模块导出模块类

标签 javascript import ecmascript-6 export

我有一个模块netmap导出默认类NetMap:

export default class NetMap {...}

我有另一个模块 helloworld,我想导出(不是作为 默认)整个 NetMap 类,以便另一个模块可以访问NetMap 使用:

import * as helloworld from 'helloworld'

const x = helloworld.NetMap()

这可能吗? NetMapexporthelloworld 模块中会是什么样子?

最佳答案

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/

相关文章:

javascript - 带有悬停状态问题的 Chrome 动画 CSS3 3D 立方体

mysql - Access MySQL 导出 XML

java - 导入语句字节码意义

javascript - 如何在 javascript 中创建动态内插字符串?

javascript - iOS 设备上 HTML5 Canvas 中 Javascript 的 setTimeout 问题

javascript - jQuery输入文件点击方法和IE上拒绝访问

javascript - 减少javascript代码

python - 由于导入机制导致 isinstance() 和 type() 等价失败(python/django)

javascript - 如何在 Angular 组件中手动运行摘要

javascript - 如何在 React Redux 应用程序中使用装饰器?