vue3 hooks函数示例


以ant-design-vue 2.2.8版Upload上传组件为例:

官方示例代码---封装前



使用hooks函数封装后



hooks函数

import { ref } from 'vue';
import { message } from 'ant-design-vue';
export default function useUpload() {
  const handleChange = (info) => {
    if (info.file.status !== 'uploading') {
      console.log(info.file, info.fileList);
    }
    
    if (info.file.status === 'done') {
      message.success(`${info.file.name} file uploaded successfully`);
    } else if (info.file.status === 'error') {
      message.error(`${info.file.name} file upload failed.`);
    }
  };
  
  const fileList = ref([]);
  return {
    fileList,
    headers: {
      authorization: 'authorization-text',
    },
    handleChange,
  };
}
vue