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

前端 未结 13 2041
陌清茗
陌清茗 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:39

    I was able to take from user atilkan's approach and modify it a bit:

    For Typescript users;

    require.context('@/folder/with/modules', false, /\.ts$/).keys().forEach((fileName => {
        import('@/folder/with/modules' + fileName).then((mod) => {
                (window as any)[fileName] = mod[fileName];
                const module = new (window as any)[fileName]();
    
                // use module
    });
    
    }));
    

提交回复
热议问题