了解的方式有两种:
1. 用 a 标签, href 设置为后端提供的 Excel 接口
<a href="excel 接口"> 导出 </a>
简单方便, 缺点就是当有 token 校验时, 不适合
2. 用 axios
把 token 放在请求的 header 里边
- import axios from 'axios'
- import { getToken } from 'js-cookie';
- methods: {
- exportExcel () {
- let url = 'http...',
- token = getToken();
- axios.get(url, {
- headers:{
- "Admin_token":token
- },
- responseType: 'blob', // 二进制流
- }).then(function (res) {
- if(!res) return
- let blob = new Blob([res.data], {type: 'application/vnd.ms-excel;charset=utf-8'})
- let url = Windows.URL.createObjectURL(blob);
- let aLink = document.createElement("a");
- aLink.style.display = "none";
- aLink.href = url;
- aLink.setAttribute("download", "excel.xls");
- document.body.appendChild(aLink);
- aLink.click();
- document.body.removeChild(aLink);
- Windows.URL.revokeObjectURL(url);
- }).catch(function (error) {
- console.log(error)
- });
- }
- }
参考: https://blog.csdn.net/xuesheng1610748/article/details/83865679
来源: http://www.bubuko.com/infodetail-2977310.html