vue3批量动态生成函数


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为自定义函数列表,是一个对象数组,传递函数名,自定义函数,非自定义函数以及其他参数


文章作者: 冷杨威
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 冷杨威 !
  目录
-->