1.http.js
import axios from 'axios'; // 引入axios
export function postDown(url, params) {
return new Promise((resolve, reject) => {
axios.post(url, params, {responseType: 'blob'})
.then(res => {
resolve(res);
})
.catch(err =>{
reject(err)
})
});
}
2.api.js
import { get, post, postDown, deletes } from './http';
let base = "";
if (process.env.NODE_ENV === "production") {
base = "";
} else {
base = "/api"
}
export const reqSpatialExcel = p => postDown(base + '/exportExcel', p);
3.methods中
// 导出表格
exportTable() {
reqSpatialExcel({
city: this.ctiy,
dateType: this.dateType,
dateTime: this.dateTime,
levelName: this.levelName,
levelType: this.levelType,
})
.then((res) => {
console.log(res)
let data = res.data
let fileName = '排名分析-' + this.dateTime
const url = window.URL.createObjectURL(
new Blob([data], {
type: 'application/vnd.ms-excel;charset=utf-8',
})
)
const link = document.createElement('a')
link.href = url
link.setAttribute('download', fileName + '.xlsx') // 需要指定文件格式。
document.body.appendChild(link)
link.click() // 点击事件
})
.catch(() => {})
}