ES6:导入许多文件 [英] ES6: import many files

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

问题描述

我有一个脚本导入大量的AMD模块,并调用每个模块的初始化方法:

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

我需要切换到ES6导入语法,我想弄清楚是否可以导入列表中的模块,类似于我的AMD代码。我想避免疯狂,如:

 从'./mod0'导入mod0; 
...
从'./modN'导入modN;

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

有关如何实现这一点的任何建议?
谢谢!

解决方案


是否可以从列表导入模块?不,没有明确地调用你的模块加载器(不管是哪一个),而不是

没有办法使用 import 声明。


有关如何这可以实现吗?


eval 可能会这样做: - ) / p>

我建议使用两个模块:

  // index.js 
从'./mod0'导出mod0;
...
从'./modN'导出modN;

  // init-all.js 
import *作为'./index'的模块; //枚举名称空间

(模块中的var moduleIdentifier)
init(modules [moduleIdentifier]);

您可能只能使用一个模块(将其自身作为模块命名空间对象导入) ,但肯定会是真正的疯狂。


I have a script that imports a lot of AMD modules and calls an initialization method on each one:

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. 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.

Any advice on how this can be accomplished?

eval could probably do it :-)

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.

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

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