阅读:2599回复:0
vue element 导出下载功能开发
request.js中定义:
let downloadLoadingInstance; export function download(url, params, filename) { downloadLoadingInstance = Loading.service({ text: "正在下载数据,请稍候", spinner: "el-icon-loading", background: "rgba(0, 0, 0, 0.7)", }) return service.post(url, params, { transformRequest: [(params) => { return tansParams(params) }], headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, responseType: 'blob' }).then(async (data) => { const isLogin = await blobValidate(data); if (isLogin) { const blob = new Blob([data]) saveAs(blob, filename) } else { const resText = await data.text(); const rspObj = JSON.parse(resText); const errMsg = errorCode[rspObj.code] || rspObj.msg || errorCode['default'] Message.error(errMsg); } downloadLoadingInstance.close(); }).catch((r) => { console.error(r) Message.error('下载文件出现错误,请联系管理员!') downloadLoadingInstance.close(); }) } 然后vue页面中引用JS 调用方法即可: // 查询参数data: queryParams: { pageNum: 1, pageSize: 10 } import {download} from '@/utils/request' download('system/post/export', { ...this.queryParams }, `post_${new Date().getTime()}.xlsx` ) |
|