【发布时间】:2018-02-25 10:47:21
【问题描述】:
有没有办法在 ES6 中使用 for-of-loop(或另一个循环)导入和导出多个文件?
const moduleNames = ['NumberUtils', 'StringUtils', 'ArrayUtils', 'MyModule', 'AnotherModule', 'BaseModule']
let modules = {}
for (const moduleName of moduleNames) {
import module from './' + moduleName
modules.moduleName = module
}
export modules
没有循环我必须写:
import NumberUtils from './NumberUtils'
import StringUtils from './StringUtils'
import ArrayUtils from './ArrayUtils'
import MyModule from './MyModule'
import AnotherModule from './AnotherModule'
import BaseModule from './BaseModule'
export {
NumberUtils,
StringUtils
ArrayUtils
MyModule
AnotherModule
BaseModule
}
【问题讨论】:
-
您可以使用
export {default as NumberUtils} from "./NumberUtils";FYI,这使得这些类型的列表更易于维护。为什么添加一行代码比向数组中添加一项更难?
标签: javascript loops import ecmascript-6