简体   繁体   English

是否有可能在javascript中迭代导入的模块?

[英]Is it possible to iterate over imported modules in javascript?

I have a common exported property, route, in all my imported modules that I would like to access dynamically, but can't work out how to iterate over the list of imported modules. 我在我想要动态访问的所有导入模块中都有一个公共导出属性route,但是无法解决如何迭代导入模块列表的问题。 Can this be done? 可以这样做吗?

import User from '/components/user.js';
import Document from '/components/document.js';

var routes = [];
[array of imported modules].forEach(m => routes.push(m.route));

UPDATE: 更新:

So far I've tried the following and can't find the module list on any of them 到目前为止,我已经尝试了以下内容,无法找到任何模块列表

console.log(User.constructor);
console.log(Reflect.ownKeys(this));
console.log(Reflect.ownKeys(User));

Where should I be looking? 我应该在哪里看?

The only way of doing that which I can think of would involve using eval(), as there are no dynamic import statements right now in js. 我能想到的唯一方法是使用eval(),因为现在js中没有动态导入语句。

I would suggest using a different library perhaps? 我建议使用不同的库? Take a look here: https://github.com/jojois74/recycle.js 看看这里: https//github.com/jojois74/recycle.js

Simply save the object that you pass to mod.prepare, and then iterate over its keys. 只需保存传递给mod.prepare的对象,然后迭代其键。

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

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