1.vue页面视图
<el-button
type="danger"
icon="el-icon-download"
size="medium"
@click="templateDownload"
>模板示例</el-button>
2.methods方法里
templateDownload() {
aoiTemplateExport().then((res) => {
const url = window.URL.createObjectURL(new Blob([res], { type: 'application/vnd.ms-excel;charset=utf-8' }))
const link = document.createElement('a')
link.style.display = 'none'
link.href = url
link.setAttribute('download', '数据模板.xlsx')
document.body.appendChild(link)
link.click()
})
}
3.封装接口
export function aoiTemplateExport() {
return request({
url: '',
method: 'get',
responseType: 'blob'
})
}
注意: 加上 responseType: ‘blob’