简体   繁体   English

ES6:导入许多文件

[英]ES6: import many files

I have a script that imports a lot of AMD modules and calls an initialization method on each one: 我有一个脚本,可以导入许多AMD模块并在每个模块上调用一种初始化方法:

define(['underscore', './mod0', ..., './modN'], function (_) {
    _.each(_.toArray(arguments).slice(1), function (m) {
        init(m);
    });
});

I need to switch to ES6 import syntax and I am trying to figure out if it is possible to import modules from a list, in a manner similar to my AMD code. 我需要切换到ES6导入语法,并且试图找出是否可以以类似于AMD代码的方式从列表中导入模块。 I want to avoid insanity like: 我想避免像这样的精神错乱:

import mod0 from './mod0';
...
import modN from './modN';

init(mod0);
...
init(modN);

Any advice on how this can be accomplished? 关于如何做到这一点的任何建议? thanks! 谢谢!

Is it possible to import modules from a list? 是否可以从列表中导入模块?

No, not without explicitly invoking your module loader (whichever that is). 不,并非没有显式调用您的模块加载器(无论是哪个)。 There is no way to do this using import declarations. 无法使用import声明来执行此操作。

Any advice on how this can be accomplished? 关于如何做到这一点的任何建议?

eval could probably do it :-) eval可能可以做到这一点:-)

I would recommend using two modules: 我建议使用两个模块:

// index.js
export mod0 from './mod0';
…
export modN from './modN';

// init-all.js
import * as modules from './index'; // enumerable namespace

for (var moduleIdentifier in modules)
    init(modules[moduleIdentifier]);

You could potentially do the same with only a single module (that imports itself as a module namespace object), but that surely would be real insanity. 您可能只用一个模块(将自身导入为模块名称空间对象)就可以做同样的事情,但是那肯定是真正的疯狂。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM