vue3 批量动态生成函数
import { ref } from 'vue'
const funcs = ref({})
function createFunction(name, body) {
funcs.value[name] = body
}
const generateFunctions = (customApiList: any) => {
if (!customApiList) return
customApiList.forEach(
(item: {
name: string,
customFn: Function,
customApi?: any,
message: any,
}) => {
if (typeof item.customFn === 'function') {
createFunction(item.name, item.customFn)
} else {
const fn = (newP: any) => {
item.customApi({ ...modalInfo.value, ...newP }).then(() => {
ElMessage({
message: item.message,
type: 'success',
})
drawerFlag.value = false
emits && emits(item.name, true)
getList()
})
}
createFunction(item.name, fn)
}
}
)
}
customApiList为自定义函数列表,是一个对象数组,传递函数名,自定义函数,非自定义函数以及其他参数