简体   繁体   中英

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

I would suggest using a different library perhaps? Take a look here: https://github.com/jojois74/recycle.js

Simply save the object that you pass to mod.prepare, and then iterate over its keys.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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