1. 导入路径
const M = {}
function importAll(r) {
r.keys().forEach(key => M[key] = r(key))
}
importAll(require.context('@/api/', true, /\.js$/))
console.log('M', M)
2. js代码
constfetchList = M[
./${this.key}.js].fetchList
先获取动态的js文件名,通过文件名获取里面的方法函数
getData() {
console.log(M)
console.log('Key', this.key)
this.listLoading = true
const fetchList = M[`./${this.key}.js`].fetchList
fetchList(this.form).then(response => {
this.list = response.data.items
this.total = response.data.total
this.listLoading = false
})
},