1. 导入路径

  1. const M = {}
  2. function importAll(r) {
  3. r.keys().forEach(key => M[key] = r(key))
  4. }
  5. importAll(require.context('@/api/', true, /\.js$/))
  6. console.log('M', M)

2. js代码

constfetchList = M[./${this.key}.js].fetchList
先获取动态的js文件名,通过文件名获取里面的方法函数

  1. getData() {
  2. console.log(M)
  3. console.log('Key', this.key)
  4. this.listLoading = true
  5. const fetchList = M[`./${this.key}.js`].fetchList
  6. fetchList(this.form).then(response => {
  7. this.list = response.data.items
  8. this.total = response.data.total
  9. this.listLoading = false
  10. })
  11. },