admin管理员组文章数量:1360330
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?
EDIT To further explain, I am importing multiple classes into a Main.js
file containing a Main class. Each of these classes es 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';
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?
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?
EDIT To further explain, I am importing multiple classes into a Main.js
file containing a Main class. Each of these classes es 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';
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?
- I'm not sure there's enough information to answer this properly. Could you expand your examples to include multiple files and exactly what you'd expect to see? – loganfsmyth Commented Apr 17, 2018 at 21:39
-
As I understand it, you want an equivalent of Node's
module.exports
/exports
object, but one containing all imports by the current file. – Rory O'Kane Commented Apr 17, 2018 at 22:22 - UPDATE: This approach satisfies what I was looking for: stackoverflow./questions/29722270/… – interwebjill Commented Jul 1, 2018 at 16:24
3 Answers
Reset to default 5There 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}};
If you need to create this variable in every file and are worried about the effort of maintaining the lists manually, you could pile the project with Babel and could write a Babel plugin to add this variable for you at the top of each file.
file.js
export firstFunc() {}
export nextFunc() {}
newFile.js
import * as Obj from "./file.js";
Does it make sense?
import {firstMethod, secondMethod} from "./firstFile";
import {anotherFirstMethod} from "./secondFile";
export {
firstMethod,
secondMethod,
anotherFirstMethod
};
or
import * as Obj from "./firstFile";
import * as Obj2 from "./secondFile";
export {
Obj,
Obj2
};
本文标签: ecmascript 6JavaScript can I access the object that contains imported modulesStack Overflow
版权声明:本文标题:ecmascript 6 - JavaScript: can I access the object that contains imported modules? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743934966a2564503.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论