从目录中的文件导入模块 [英] import modules from files in directory
问题描述
import {ThingA,ThingB,ThingC} from es6,我可以从这样一个文件导入多个导出: 'LIB /事情';
但是,我喜欢组织每个文件有一个模块。我最终得到这样的导入:
从'lib / things / ThingA'导入ThingA;
从'lib / things / ThingB'导入ThingB;
从'lib / things / ThingC'导入ThingC;
我希望能够这样做:
$ b $来自'lib / things / *'的b
import {ThingA,ThingB,ThingC};或者类似的东西,通过理解的约定,每个文件都包含一个默认导出,并且每个模块被命名为
<与其文件一样。
这是可能吗?
我不认为这是可能的,但是对模块名称的解决方案取决于模块加载程序,因此可能会有一个加载器实现支持这一点。
在此之前,您可以在 lib / things / index.js
中使用一个中间的模块文件,其中包含
export'from'ThingA';
export * from'ThingB';
export * from'ThingC';
,它将允许您执行
<$来自'lib / thing'的p $ p>
import {ThingA,ThingB,ThingC};
With es6, I can import several exports from a file like this:
import {ThingA, ThingB, ThingC} from 'lib/things';
However, I like the organization of having one module per file. I end up with imports like this:
import ThingA from 'lib/things/ThingA';
import ThingB from 'lib/things/ThingB';
import ThingC from 'lib/things/ThingC';
I would love to be able to do this:
import {ThingA, ThingB, ThingC} from 'lib/things/*';
or something similar, with the understood convention that each file contains one default export, and each module is named the same as its file.
Is this possible?
I don't think this is possible, but afaik the resolution of module names is up to module loaders so there might a loader implementation that does support this.
Until then, you could use an intermediate "module file" at lib/things/index.js
that just contains
export * from 'ThingA';
export * from 'ThingB';
export * from 'ThingC';
and it would allow you to do
import {ThingA, ThingB, ThingC} from 'lib/things';
这篇关于从目录中的文件导入模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!