require.context

Traversing directories for batch asynchronous imports is especially useful in the scenario of massive imports

parameter

  1. Directory {String} – Read the file path
  2. UseSubdirectories {Boolean} – Whether to traverse a file subdirectory
  3. RegExp {regExp} – Matches the re of the file

return

Returns a function with three attributes

  1. Resolve is a function that returns the module ID after the request is resolved.
  2. Keys is a function that returns an array of all the requests that might be processed by the context module.
  3. Id is the module ID contained in the context module. It may be used when you use module.hot.accept

The module is obtained by calling the key passed in by the function itself

let contexts = require.context('. '.false./\.vue$/)
contexts.keys().forEach(key= > {
    // The module's default export
    let moduleExport = contexts(key).default
})
Copy the code