[英]Why in Firefox Addon the imported objects can not access the importers' imported modules?
[英]JavaScript: can I access the object that contains imported modules?
我想创建一个要导入Main.js
文件的模块数组。 如何访问包含导入模块的对象?
编辑为了进一步说明,我将多个类导入包含Main类的Main.js
文件中。 这些类的每一个都来自各自的文件,为我提供了如下的导入语句:
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 ]
? 还是可以创建一个?
没有使用当前文件的导入自动填充的数据结构。 您将必须构造自己的数组或对象,以手动列出每个导入。
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插件在每个文件的顶部为您添加此变量。
file.js
export firstFunc() {}
export nextFunc() {}
newFile.js
import * as Obj from "./file.js";
是否有意义?
import {firstMethod, secondMethod} from "./firstFile";
import {anotherFirstMethod} from "./secondFile";
export {
firstMethod,
secondMethod,
anotherFirstMethod
};
要么
import * as Obj from "./firstFile";
import * as Obj2 from "./secondFile";
export {
Obj,
Obj2
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.