1、下载插件 axios 和 qs
npm install --save axios,qs
2、 引用,调用接口, 下载文件
import axios from 'axios';
import QS from 'qs'
methods:{
// url 地址和 params 传参使用自己的 ,excelName文件名称
axios({
method: 'get',
url:this.url,
params: this.params,
paramsSerializer: function (params) {
return QS.stringify(params, {
arrayFormat: 'repeat'
})
},
responseType: 'blob',
}).then((res) => {
const filename = this.excelName;
const fileContent = res.data;
// application/vnd.ms-excel;charset=utf8
// 创建 Blob 对象
const blob = new Blob([fileContent], { type: "application/vnd.ms-excel;charset=utf8" });
// 创建下载链接
const downloadUrl = URL.createObjectURL(blob);
// 创建下载链接的 <a> 元素
const downloadLink = document.createElement("a");
downloadLink.href = downloadUrl;
downloadLink.download = filename;
// 模拟点击下载链接
downloadLink.click();
// 清理创建的 URL 对象
URL.revokeObjectURL(downloadUrl);
});
}
使用上面函数即可完成下载功能
3、 在调试过程中发现一个问题 Excel表格下载打开后为[objet][objet]
原因是把res.data写成了res,修改之后再次导出就好了
返回数据为