1. 前置条件:后台提供导出接口,接口返回的是文件地址("file_url": "")
2.代码:
<template>
<el-button @click="onClickExport">导出</el-button>
</template>
<script>
import api from '../../api/api.js' // 封装接口的文件
export default {
methods: {
/** 导出 */
onClickExport() {
api.listDownload(this.form).then(res => { // listDownload就是导出接口的名字
// 接口请求成功之后获取file_url,然后执行下载操作
const a = document.createElement('a')
a.download = '文件名.xlsx'
a.href = res.n.file_url
document.body.appendChild(a)
a.click()
document.body.removeChild(a)
})
}
}
}
</script>