require.context
Traversing directories for batch asynchronous imports is especially useful in the scenario of massive imports
parameter
- Directory {String} – Read the file path
- UseSubdirectories {Boolean} – Whether to traverse a file subdirectory
- RegExp {regExp} – Matches the re of the file
return
Returns a function with three attributes
- Resolve is a function that returns the module ID after the request is resolved.
- Keys is a function that returns an array of all the requests that might be processed by the context module.
- 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