接口地址 /export/getExcel
跨域原因,需要代理一下
vue.config.js
proxy:{
‘/export’:{
target:‘服务地址’,
changeOrigin:true
}
}
export.vue
<button @click="exportData">导出</button>
methods:{
exportData(){
// 入参
let params = {
startDate:'',
endData:''
}
axios('/export/getExcel',{
method:'POST',
data:params,
responseType:'blob'
}).then((response)=>{
//文件名
const filename = response.headers['content-dispoaition'].split(';')[1].split('=')[1]
const blob = new Blob([response.data])
const link = document.createElement('a')
link.download = filename
link.style.display = 'none'
link.href = URL.createObjURL(blob)
document.body.appendChild(link)
link.click()
URL.revokeObjectURL(link.href)
document.body.removeChild(link)
})
}
}```