导出

方法一:

// js文件
/*
* * post通用导出方法 * @param {url} url * @param {data} 参数 * @param {name} 表格名称 */ export function deriveList(url, data, name) { axios({ method: 'post', url: `${process.env.VUE_APP_BASE_API}${url}`, headers: { 'content-type': 'application/json', 'token': getToken() // token换成从缓存获取 }, responseType: 'blob', data }).then(res => { var disposition = res.headers['content-disposition']; var fileName = decodeURI(disposition.substring(disposition.indexOf('filename=') + 9, disposition.length)); let blob = new Blob([res.data], { type: 'application/vnd.ms-excel' }); if (window.navigator && window.navigator.msSaveOrOpenBlob) { navigator.msSaveBlob(blob, fileName) } else { const link = document.createElement('a'); link.style.display = 'none'; var href = URL.createObjectURL(blob); link.href = href; link.setAttribute('download', `${name}.xlsx`); document.body.appendChild(link); link.click(); // 释放掉blob对象 document.body.removeChild(link); // 释放掉blob对象 window.URL.revokeObjectURL(href); // 释放掉blob对象 } }).catch((error) => { console.error(error); }) }

方法二:

<div @click="handleExport"></div>

handleExport() {
      window.open(
        `${process.env.VUE_APP_BASE_API}/导出接口` +
          "&token=" + getToken()
      );
},

 

posted @ 2022-05-19 16:51  挽你手  阅读(572)  评论(0编辑  收藏  举报