<el-button @click="exportExcel()">导出</el-button>
<script>
methods: {
exportExcel(){
var params={
XX:xx//额外需要携带的请求体
}
this.$axios.get('/API/exportExcel',{
params: params,
responseType: 'blob' //首先设置responseType字段格式为 blob
}).then(res => {
if (!res) {
this.$message.error("下载模板文件失败");
return false;
}
let blob = new Blob([res], {type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;"}); // 为blob设置文件类型,这里以.xlsx为例,[res]这个根据后台数据来,有可能是包在res.data
let url = window.URL.createObjectURL(blob); // 创建一个临时的url指向blob对象
let a = document.createElement("a");
a.href = url;
a.download = '你所起的文件名';
a.click();
// 释放这个临时的对象url
window.URL.revokeObjectURL(url);
});
},
}
</script>