Vue3界面导出按钮加一个等待提示
时间: 2025-06-22 19:41:20 浏览: 18
### Vue3 中实现导出按钮点击时显示等待提示
为了提升用户体验,在执行耗时操作如导出大量数据至 Excel 文件前展示等待提示是非常必要的。对于 Vue3 项目而言,可以利用 `ref` 或者 `reactive` 来管理组件的状态变化,并通过条件渲染的方式控制加载状态的显示与隐藏。
下面是一个基于 Element Plus 组件库的例子,展示了如何创建一个带有等待提示功能的导出按钮:
#### HTML 部分
```html
<template>
<!-- 导出按钮 -->
<el-button type="primary" size="small" @click="handleExportClick" :loading="exportLoading">
{{ exportLoading ? '正在导出...' : '导出Excel'}}
</el-button>
<!-- 等待对话框 (可选) -->
<el-dialog v-model="dialogVisible" title="文件准备中..." width="30%">
<span>请稍候...</span>
<div style="text-align:center;margin-top:20px;">
<i class="el-icon-loading"></i>
</div>
</el-dialog>
</template>
```
#### JavaScript/TypeScript 部分
```javascript
<script setup lang="ts">
import { ref } from "vue";
// 假设这里已经定义好了发送请求的方法 sendRequestToServer 和 处理返回的数据 downloadFile 方法
const exportLoading = ref(false); // 控制按钮 loading 状态
const dialogVisible = ref(false); // 控制弹窗可见性
async function handleExportClick() {
try {
exportLoading.value = true;
dialogVisible.value = true;
const response = await sendRequestToServer(); // 发送 POST 请求获取 Excel 数据流
if (!response.ok) throw new Error('服务器错误');
await downloadFile(response.data);
} catch(error){
console.error("导出失败:", error.message);
} finally{
setTimeout(() => {
exportLoading.value = false;
dialogVisible.value = false;
}, 1500); // 模拟延迟关闭动画效果
}
}
</script>
```
此代码片段实现了当用户点击“导出Excel”按钮时会触发 `handleExportClick()` 函数,该函数负责设置按钮进入加载模式并打开等待对话框;随后发起异步 HTTP 请求以获得 Excel 文件的内容;最后调用 `downloadFile()` 下载文件给客户端。无论成功与否都会重置 UI 的加载指示器[^1]。
阅读全文
相关推荐












