是否可以使用通配符从目录中的所有文件导入模块?使用ES6,我可以从这样的文件中导入几个导出:import {ThingA, ThingB, ThingC} from 'lib/things';但是,我喜欢每个文件都有一个模块的组织。我最终得到了这样的进口产品:import ThingA from 'lib/things/ThingA';import ThingB from 'lib/things/ThingB';import ThingC from 'lib/things/ThingC';我很乐意这样做:import {ThingA, ThingB, ThingC} from 'lib/things/*';或者类似的东西,使用理解的约定,即每个文件包含一个默认导出,并且每个模块的名称与其文件相同。这个是可能的吗?
3 回答
牛魔王的故事
TA贡献1830条经验 获得超3个赞
lib/things/index.js
export * from 'ThingA';export * from 'ThingB';export * from 'ThingC';
import {ThingA, ThingB, ThingC} from 'lib/things';
撒科打诨
TA贡献1934条经验 获得超2个赞
Thing
,
export default function ThingA () {}
things/index.js
,
export {default as ThingA} from './ThingA'export {default as ThingB} from './ThingB'export {default as ThingC} from './ThingC'
import * as things from './things'things.ThingA()
import {ThingA,ThingB} from './things'
幕布斯6054654
TA贡献1876条经验 获得超7个赞
babel
npm i --save-dev babel-plugin-wildcard
.babelrc
{ "plugins": ["wildcard"]}
import * as Things from './lib/things';// Do whatever you want with these :DThings.ThingA;Things.ThingB;Things.ThingC;
index.js
readdir
import { ThingsA, ThingsB, ThingsC } from './lib/things/*';
添加回答
举报
0/150
提交
取消