JavaScript:我可以访问包含导入模块的对象吗? [英] JavaScript: can I access the object that contains imported modules?

查看:68
本文介绍了JavaScript:我可以访问包含导入模块的对象吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个要导入到Main.js文件中的模块的数组.我如何访问包含导入模块的对象?

I'd like to create an array of the modules I am importing into Main.js file. How might I access the object that contains the imported modules?

编辑为了进一步说明,我将多个类导入到包含Main类的Main.js文件中.这些类的每一个都来自各自的文件,为我提供了如下的导入语句:

EDIT To further explain, I am importing multiple classes into a Main.js file containing a Main class. Each of these classes comes from their own individual file, giving me import statements like this:

import Header from './features/Header.js';
import ImageStrip from './features/ImageStrip.js';
import AreaChart from './features/AreaChart.js';
import Axes from './features/Axes.js';

是否有一个JavaScript对象,其中包含导入的模块,例如[ Header, ImageStrip, AreaChart, Axes ]之类的东西?还是可以创建一个?

Is there a JavaScript object that contains the modules that are imported, for example something like [ Header, ImageStrip, AreaChart, Axes ]? Or is it possible to create one?

推荐答案

没有使用当前文件的导入自动填充的数据结构.您将必须构造自己的数组或对象,以手动列出每个导入.

There is no data structure that is automatically populated with the imports of the current file. You will have to construct your own array or object that manually lists each import.

import Feature1 from './features/Feature1.js';
import Feature2 from './features/Feature2.js';
import {SubFeatureX} from './features/Feature3.js';

const imports = [Feature1, Feature2, {SubFeatureX}];
// or
const imports = {Feature1, Feature2, Feature3: {SubFeatureX}};

如果您需要在每个文件中创建此变量,并且担心手动维护列表的工作,则可以使用编写一个Babel插件,以便在每个文件的顶部为您添加此变量.

If you need to create this variable in every file and are worried about the effort of maintaining the lists manually, you could compile the project with Babel and could write a Babel plugin to add this variable for you at the top of each file.

这篇关于JavaScript:我可以访问包含导入模块的对象吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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