Home > Blockchain >  Vue.js - How to loop through a folder
Vue.js - How to loop through a folder

Time:10-28

I have a Vue SPA app. Now I want to extend this app with app modules so I thought to make a folder src/modules that contain the modules folders. I want to put at the root of each folder some configuration js files (store.js, route.js, and so on). The question is: in the main files (src/store/index.js, src/router/index.js, etc.), how can I loop for every folder in src/modules and check if some configuration file exists to add its content at the main config? Of course this operation should be executed at build time, modules install is not intended to be dynamically. Thanks a lot

CodePudding user response:

You can use require.context

const configurations = require.context('./modules', true, /config\.js$/i);
configurations.keys().forEach(filename =>
{
  console.log(filename);
  // file contents will be inside configurations[filename]
});
  • Related