是否可以使用通配符从目录中的所有文件导入模块? [英] Is it possible to import modules from all files in a directory, using a wildcard?

查看:96
本文介绍了是否可以使用通配符从目录中的所有文件导入模块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用ES6,我可以从这样的文件中导入几个导出:

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.

这可能吗?

推荐答案

我不认为这是可能的,但是模块名称的解析取决于模块加载器,所以可能有一个加载器实现支持这个。

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.

在此之前,你可以在 lib / things / index.js 中使用一个中间的模块文件,它只包含

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

并允许你这样做

import {ThingA, ThingB, ThingC} from 'lib/things';

这篇关于是否可以使用通配符从目录中的所有文件导入模块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆