循环中的ES6导入 [英] ES6 import in for-of-loop

查看:131
本文介绍了循环中的ES6导入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在ES6中使用for-of-loop(或其他循环)导入和导出多个文件?

Is there any way to import and export multiple files using for-of-loop (or another loop) in 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
}


推荐答案

对于多个导入文件,我找到了这个解决方案:

For multiple import files I found this solution:

const files = require.context('../myFolder', true, /(Module|Utils)\.js$/)

这篇关于循环中的ES6导入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆