vue使用require.context

const files = require.context(
    '/',
    false,
    /\.js$/
)

const obj = {}

files.keys().forEach(item => {
    if (item === './index.js') return
    let name = item.split('/').pop().replace(/\.\w+$/, '')
    let upperName = name.replace(/\w/, s => s.toUpperCase())
    obj[upperName] = files(item)['default']
})

export default obj

require-context

推荐阅读更多精彩内容