有什麼方法可以在ES6中使用迴路(或其他迴路)導入和導出多個文件?ES6在迴路中導入
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
}
你可以做'從「./NumberUtils」出口{默認爲NumberUtils};'僅供參考,這使得這些類型的列表更容易保持。爲什麼添加一行代碼比向數組添加項目更困難? – loganfsmyth