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

时间:2018-04-17 19:14:52

标签: javascript ecmascript-6 module

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

编辑为了进一步说明,我将多个类导入到包含Main类的Main.js文件中。这些类中的每一个都来自它们自己的单独文件,给我这样的import语句:

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 ]之类的东西?或者是否可以创建一个?

3 个答案:

答案 0 :(得分:1)

没有数据结构会自动填充当前文件的导入。您必须构建自己的数组或对象,手动列出每个导入。

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编译项目,并write a Babel plugin可以为您添加此变量每个文件的顶部。

答案 1 :(得分:0)

[reactor-http-nio-4] WARN io.netty.util.concurrent.AbstractEventExecutor - A task raised an exception. Task: reactor.ipc.netty.channel.ContextHandler$$Lambda$401/11401686@10ae5a2 reactor.core.Exceptions$ErrorCallbackNotImplemented: io.netty.handler.codec.EncoderException: java.lang.OutOfMemoryError Caused by: io.netty.handler.codec.EncoderException: java.lang.OutOfMemoryError at io.netty.handler.codec.MessageToMessageEncoder.write(MessageToMessageEncoder.java:106) at io.netty.channel.CombinedChannelDuplexHandler.write(CombinedChannelDuplexHandler.java:348) at io.netty.channel.AbstractChannelHandlerContext.invokeWrite0(AbstractChannelHandlerContext.java:738) at io.netty.channel.AbstractChannelHandlerContext.invokeWrite(AbstractChannelHandlerContext.java:730) at io.netty.channel.AbstractChannelHandlerContext.write(AbstractChannelHandlerContext.java:816) at io.netty.channel.AbstractChannelHandlerContext.write(AbstractChannelHandlerContext.java:723) at reactor.ipc.netty.channel.ChannelOperationsHandler.doWrite(ChannelOperationsHandler.java:291) at reactor.ipc.netty.channel.ChannelOperationsHandler.drain(ChannelOperationsHandler.java:465) at reactor.ipc.netty.channel.ChannelOperationsHandler.flush(ChannelOperationsHandler.java:191) at io.netty.channel.AbstractChannelHandlerContext.invokeFlush0(AbstractChannelHandlerContext.java:776) at io.netty.channel.AbstractChannelHandlerContext.invokeWriteAndFlush(AbstractChannelHandlerContext.java:802) at io.netty.channel.AbstractChannelHandlerContext.write(AbstractChannelHandlerContext.java:814) at io.netty.channel.AbstractChannelHandlerContext.writeAndFlush(AbstractChannelHandlerContext.java:794) at io.netty.channel.AbstractChannelHandlerContext.writeAndFlush(AbstractChannelHandlerContext.java:831) at io.netty.channel.DefaultChannelPipeline.writeAndFlush(DefaultChannelPipeline.java:1051) at io.netty.channel.AbstractChannel.writeAndFlush(AbstractChannel.java:300) at reactor.ipc.netty.http.HttpOperations.lambda$then$0(HttpOperations.java:135) at reactor.ipc.netty.FutureMono.lambda$deferFuture$0(FutureMono.java:68) at reactor.ipc.netty.FutureMono$DeferredFutureMono.subscribe(FutureMono.java:134) at reactor.core.publisher.Mono.subscribe(Mono.java:3008) at reactor.core.publisher.MonoIgnoreThen$ThenIgnoreMain.drain(MonoIgnoreThen.java:167) at reactor.core.publisher.MonoIgnoreThen.subscribe(MonoIgnoreThen.java:56) at reactor.core.publisher.Mono.subscribe(Mono.java:3008) at reactor.core.publisher.FluxConcatIterable$ConcatIterableSubscriber.onComplete(FluxConcatIterable.java:141) at reactor.core.publisher.FluxConcatIterable.subscribe(FluxConcatIterable.java:60) at reactor.core.publisher.MonoSourceFlux.subscribe(MonoSourceFlux.java:47) at reactor.ipc.netty.channel.ChannelOperations.applyHandler(ChannelOperations.java:380) at reactor.ipc.netty.http.client.HttpClientOperations.onHandlerStart(HttpClientOperations.java:501) at io.netty.util.concurrent.AbstractEventExecutor.safeExecute(AbstractEventExecutor.java:163) at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:404) at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:463) at io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:886) at java.lang.Thread.run(Thread.java:745) Caused by: java.lang.OutOfMemoryError at sun.misc.Unsafe.allocateMemory(Native Method) at io.netty.util.internal.PlatformDependent0.allocateDirectNoCleaner(PlatformDependent0.java:430) at io.netty.util.internal.PlatformDependent.allocateDirectNoCleaner(PlatformDependent.java:596) at io.netty.buffer.PoolArena$DirectArena.allocateDirect(PoolArena.java:764) at io.netty.buffer.PoolArena$DirectArena.newChunk(PoolArena.java:740) at io.netty.buffer.PoolArena.allocateNormal(PoolArena.java:244) at io.netty.buffer.PoolArena.allocate(PoolArena.java:214) at io.netty.buffer.PoolArena.allocate(PoolArena.java:146) at io.netty.buffer.PooledByteBufAllocator.newDirectBuffer(PooledByteBufAllocator.java:324) at io.netty.buffer.AbstractByteBufAllocator.directBuffer(AbstractByteBufAllocator.java:185) at io.netty.buffer.AbstractByteBufAllocator.directBuffer(AbstractByteBufAllocator.java:176) at io.netty.buffer.AbstractByteBufAllocator.buffer(AbstractByteBufAllocator.java:113) at io.netty.handler.codec.http.HttpObjectEncoder.encode(HttpObjectEncoder.java:92) at io.netty.handler.codec.http.HttpClientCodec$Encoder.encode(HttpClientCodec.java:167) at io.netty.handler.codec.MessageToMessageEncoder.write(MessageToMessageEncoder.java:88) ... 32 more
file.js
export firstFunc() {} export nextFunc() {}
newFile.js

有意义吗?

答案 2 :(得分:0)

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
};