Is it possible to import modules from all files in a directory, using a wildcard?

前端 未结 13 2063
陌清茗
陌清茗 2020-11-22 04:25

With ES6, I can import several exports from a file like this:

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

However, I like the orga

13条回答
  •  再見小時候
    2020-11-22 04:44

    Just a variation on the theme already provided in the answer, but how about this:

    In a Thing,

    export default function ThingA () {}
    

    In things/index.js,

    export {default as ThingA} from './ThingA'
    export {default as ThingB} from './ThingB'
    export {default as ThingC} from './ThingC'
    

    Then to consume all the things elsewhere,

    import * as things from './things'
    things.ThingA()
    

    Or to consume just some of things,

    import {ThingA,ThingB} from './things'
    

提交回复
热议问题