• vue3 hooks demo


    export default function () {
      let point = reactive({ x: 0, y: 0 })
      function usePoint(e) {
        point.x = e.x
        point.y = e.y
      }
      onMounted(() => {
        window.addEventListener('mousemove', usePoint)
      })
      onBeforeUnmount(() => {
        window.removeEventListener('mousemove', usePoint)
      })
      return point
    }
    
    import { ElNotification } from "element-plus";
    /**
     * @description 接收数据流生成 blob,创建链接,下载文件
     * @param {any} data 导出的文件blob数据 (必传)
     * @param {String} tempName 导出的文件名 (必传)
     * @param {Boolean} isNotify 是否有导出消息提示 (默认为 true)
     * @param {String} fileType 导出的文件格式 (默认为.xlsx)
     * */
    interface useDownloadParam {
      data: any;
      tempName: string;
      isNotify?: boolean;
      fileType?: string;
    }
    //下载
    export const useDownload = async ({ data, tempName,isNotify = true, fileType = ".xlsx" }: useDownloadParam) => {
      if (isNotify) {
        ElNotification({
          title: "温馨提示",
          message: "如果数据庞大会导致下载缓慢哦,请您耐心等待!",
          type: "info",
          duration: 3000
        });
      }
      try {
        const blob = new Blob([data]);
        // 兼容 edge 不支持 createObjectURL 方法
        if ("msSaveOrOpenBlob" in navigator) return window.navigator.msSaveOrOpenBlob(blob, tempName + fileType);
        const blobUrl = window.URL.createObjectURL(blob);
        const exportFile = document.createElement("a");
        exportFile.style.display = "none";
        exportFile.download = `${tempName}${fileType}`;
        exportFile.href = blobUrl;
        document.body.appendChild(exportFile);
        exportFile.click();
        // 去除下载对 url 的影响
        document.body.removeChild(exportFile);
        window.URL.revokeObjectURL(blobUrl);
      } catch (error) {
        console.log(error);
      }
    };
    //组件中使用
    <script setup lang="ts">
    import { useDownload } from "@/hooks/useDownload";
    
    const userForm = reactive({})
    const userListExport = () => {
        new Promise(resolve => {
            $Request({
                url: $Urls.userListExport,
                method: "post",
                data: userForm,
                responseType: "blob"
            }).then((res: any) => {
                useDownload({
                    data: res.data,   
                    tempName:"用户列表"      
                });
                resolve(res);
            });
        });
    };
    </script>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
  • 相关阅读:
    Steger算法实现结构光光条中心提取(python版本)
    Nginx -- SSL模块
    VisualDL的认识
    vue3+element-plus 封装列表页,分页,排序,导出
    【Transformer Based Cls&Det】Transformer系列分类和检测网络原理和源码讲解导航
    指数期货品种联动(指数期货类型)
    OAuth 2.0实现统一认证
    企业信息化建设搞得好了叫系统工程,搞不好叫面子工程
    6.3 公钥算法的基本数论知识(欧几里得算法、扩展欧几里得算法、欧拉函数、费马小定理与欧拉定理)
    推荐系统-指标:ctr、cvr
  • 原文地址:https://blog.csdn.net/weCat_s/article/details/133643767