vue+axios實(shí)現(xiàn)文件下載及vue中使用axios的實(shí)例
功能:點(diǎn)擊導(dǎo)出按鈕,提交請(qǐng)求,下載excel文件;
第一步:跟后端童鞋確認(rèn)交付的接口的response header設(shè)置了
以及返回了文件流。
第二步:修改axios請(qǐng)求的responseType為blob,以post請(qǐng)求為例:
axios({ method: 'post', url: 'api/user/', data: { firstName: 'Fred', lastName: 'Flintstone' }, responseType: 'blob' }).then(response => { this.download(response) }).catch((error) => { })
第三步:請(qǐng)求成功,拿到response后,調(diào)用download函數(shù)(創(chuàng)建a標(biāo)簽,設(shè)置download屬性,插入到文檔中并click)
methods: { // 下載文件 download (data) { if (!data) { return } let url = window.URL.createObjectURL(new Blob([data])) let link = document.createElement('a') link.style.display = 'none' link.href = url link.setAttribute('download', 'excel.xlsx') document.body.appendChild(link) link.click() } }
下面在通過(guò)實(shí)例代碼看下vue中使用axios
1.安裝axios
npm:
$ npm install axios -S
cdn:
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
2.配置axios
在項(xiàng)目中新建api/index.js文件,用以配置axios
api/index.js
import axios from 'axios'; let http = axios.create({ baseURL: 'http://localhost:8080/', withCredentials: true, headers: { 'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8' }, transformRequest: [function (data) { let newData = ''; for (let k in data) { if (data.hasOwnProperty(k) === true) { newData += encodeURIComponent(k) + '=' + encodeURIComponent(data[k]) + '&'; } } return newData; }] }); function apiAxios(method, url, params, response) { http({ method: method, url: url, data: method === 'POST' || method === 'PUT' ? params : null, params: method === 'GET' || method === 'DELETE' ? params : null, }).then(function (res) { response(res); }).catch(function (err) { response(err); }) } export default { get: function (url, params, response) { return apiAxios('GET', url, params, response) }, post: function (url, params, response) { return apiAxios('POST', url, params, response) }, put: function (url, params, response) { return apiAxios('PUT', url, params, response) }, delete: function (url, params, response) { return apiAxios('DELETE', url, params, response) } }
這里的配置了POST、GET、PUT、DELETE方法。并且自動(dòng)將JSON格式數(shù)據(jù)轉(zhuǎn)為URL拼接的方式
同時(shí)配置了跨域,不需要的話將withCredentials設(shè)置為false即可
并且設(shè)置了默認(rèn)頭部地址為:http://localhost:8080/,這樣調(diào)用的時(shí)候只需寫(xiě)訪問(wèn)方法即可
3.使用axios
注:PUT請(qǐng)求默認(rèn)會(huì)發(fā)送兩次請(qǐng)求,第一次預(yù)檢請(qǐng)求不含參數(shù),所以后端不能對(duì)PUT請(qǐng)求地址做參數(shù)限制
首先在main.js中引入方法
import Api from './api/index.js'; Vue.prototype.$api = Api;
然后在需要的地方調(diào)用即可
this.$api.post('user/login.do(地址)', { "參數(shù)名": "參數(shù)值" }, response => { if (response.status >= 200 && response.status < 300) { console.log(response.data);\\請(qǐng)求成功,response為成功信息參數(shù) } else { console.log(response.message);\\請(qǐng)求失敗,response為失敗信息 } });
總結(jié)
以上所述是小編給大家介紹的vue+axios實(shí)現(xiàn)文件下載及vue中使用axios的實(shí)例,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
Vue包大小優(yōu)化的實(shí)現(xiàn)(從1.72M到94K)
這篇文章主要介紹了Vue包大小優(yōu)化的實(shí)現(xiàn)(從1.72M到94K),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02解決Element組件的坑:抽屜drawer和彈窗dialog
這篇文章主要介紹了解決Element組件的坑:抽屜drawer和彈窗dialog問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-07-07Vue自定義render統(tǒng)一項(xiàng)目組彈框功能
這篇文章主要介紹了Vue自定義render統(tǒng)一項(xiàng)目組彈框功能,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-06-06uniapp嵌套webview無(wú)法返回上一級(jí)解決方式
uniapp是一款非常強(qiáng)大的跨平臺(tái)開(kāi)發(fā)框架,它可以讓我們只編寫(xiě)一份代碼,就能在多個(gè)平臺(tái)上運(yùn)行,這篇文章主要給大家介紹了關(guān)于uniapp嵌套webview無(wú)法返回上一級(jí)的解決方式,需要的朋友可以參考下2024-05-05vue中實(shí)現(xiàn)methods一個(gè)方法調(diào)用另外一個(gè)方法
下面小編就為大家分享一篇vue中實(shí)現(xiàn)methods一個(gè)方法調(diào)用另外一個(gè)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-02-0256個(gè)實(shí)用的JavaScript 工具函數(shù)助你提升開(kāi)發(fā)效率
今天來(lái)看看JavaScript中的一些實(shí)用的工具函數(shù),希望能幫助你提高開(kāi)發(fā)效率!需要的朋友可以參考下面文章的具體內(nèi)容2021-10-10Vue開(kāi)發(fā)實(shí)例探究key的作用詳解
這篇文章主要為大家介紹了Vue開(kāi)發(fā)實(shí)例探究key的作用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01關(guān)于SpringBoot與Vue交互跨域問(wèn)題解決方案
最近在利用springboot+vue整合開(kāi)發(fā)一個(gè)前后端分離的個(gè)人博客網(wǎng)站,所以這一篇總結(jié)一下在開(kāi)發(fā)中遇到的一個(gè)問(wèn)題,關(guān)于解決在使用vue和springboot在開(kāi)發(fā)前后端分離的項(xiàng)目時(shí),如何解決跨域問(wèn)題。在這里分別分享兩種方法,分別在前端vue中解決和在后臺(tái)springboot中解決。2021-10-10vue中使用v-if隱藏元素時(shí)會(huì)出現(xiàn)閃爍問(wèn)題的解決
這篇文章主要介紹了vue中使用v-if隱藏元素時(shí)會(huì)出現(xiàn)閃爍問(wèn)題的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04