后端返回数据格式:
重新定义一个api
import axios from 'axios'
export function exportFun(data) {
axios({
method: data.method,
url:data.url,
params:data.params,
data:data.data,
responseType: 'blob',
}).then((res) => {
const link = document.createElement('a')
let blob = new Blob([res.data], {type: 'application/vnd.ms-excel'})
link.style.display = 'none'
link.href = URL.createObjectURL(blob)
let fullName=res.headers['content-disposition'].split('=')
link.download = data.fileName|| fullName[fullName.length-1]//下载后文件名
document.body.appendChild(link)
link.click()
document.body.removeChild(link)
}).catch(error => {
console.log(error)
})
}
使用
exportMethod({
method: 'post',
url: 'url',
params:this.excelForm,
fileName:`hello.xlsx`
})```