axios请求函数
// 导出
export const exportInfo = (data) => {
return request.post({
url: `请求地址`,
data,//请求参数
responseType: 'blob'// 重点,responseType设置为 blob
})
}
页面导出函数
// 导出
const downloadExcel = () => {
ElMessageBox.confirm('确定要导出吗', '导出', {
confirmButtonText: '确认',
cancelButtonText: '取消',
type: 'warning'
}).then(async () => {
const res = await exportInfo(searchParams)
if (res) {
const elink = document.createElement('a')
elink.download = '列表.xls'
elink.style.display = 'none'
const blob = new Blob([res], { type: 'application/x-msdownload' })
elink.href = URL.createObjectURL(blob)
document.body.appendChild(elink)
elink.click()
document.body.removeChild(elink)
ElMessage({
type: 'success',
message: '导出成功'
})
}
})
}
这样就可以导出啦~