0%

引用文件夹下所有js

在index.js中export同级js文件中暴露的数据:

1
2
3
4
5
6
7
8
9
// index.js 
const hooks = {}
const context = require.context('./', false, /\.js$/)
const keys = context.keys().filter(item => item !== './index.js')
keys.forEach(filePath => {
const file = context(filePath).default
Object.assign(hooks, { [file.name]: file })
})
export default hooks

使用方式:

1
2
import hooks from 'xxx/xxx/index.js' 
const { xxx, xxx, xxx } = hooks