JavaScript:我可以访问包含导入模块的对象吗?

编程入门 行业动态 更新时间:2024-10-28 15:32:49
本文介绍了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 并可以编写一个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:我可以访问包含导入模块的对象吗?

本文发布于:2023-06-07 13:25:11,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/560842.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:模块   对象   JavaScript

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!