I don’t know if you keep importing when registering global components. I really want to write a loop to register public components. The require.context()
function can satisfy this wish.
It is a webpack API, when we want to import multiple files from a folder, we can use it, it will automatically traverse the specified files in the specified folder
The require.context()
function receives three parameters
const files = require.context('@/components', true, /index\.vue$/)
'@/components'
: specify the folder path to traversetrue
: whether to traverse the subdirectories of the file/index\.vue$/
: the regular expression that matches the fileAfter the require.context()
function is executed, it returns a function, which has three attributes
File Directory
Traverse the index.vue
file in the subdirectory of the components folder and mount the component to Vue
Console output:
Introduced under the main.js folder