axios知識點總結

2021-09-07 05:18:25 字數 2538 閱讀 8465

axios 是乙個基於 promise 的 http 庫,可以用在瀏覽器和 node.js 中。在vue中常用axios來做前後端的互動;

上一張git上的star值

使用安裝:$ npm install axios cdn方式: src=「

基本使用:

axios.method('url',[...data],options)

.then(function(res){})

.catch(function(err){})

合併請求:

例如:

//定義兩個不不同的情請求

var q1 =this.axio.get(『url』)

var q2= this.post('url','a=1')

//合併q1,q2請求

this.$axios.all([q1,q2])

.then(this.$axios.spread((res1,res2)=>))

.catch(err=>)

配置公共資訊:

this.$ axios.defaults.baseurl = 『http://sss/ss/s』
options引數還可以為params查詢字串物件;transformrequest轉換請求體資料;headers請求頭資訊;data請求資料,timeout請求超時

場景:所有請求自帶的頭資訊

this.$axios.headers ={};// 覆蓋原本預設的頭部

this.$ axios.defaults.headers.accept ='abc'//修改個別資訊即可

沒有使用箭頭函式的時候,一定要注意作用域的問題,如上訴**的that

登入的安全機制

xsrfcookiename伺服器返回乙個xsrf-token令牌,儲存起來

xsrfheadername 請求自動攜帶x-xsrf-token=***

取消請求 斷點續傳

取消請求

場景:在上傳檔案的時候,使用者發現去、檔案錯誤取消請求

const canceltoken = axios.canceltoken;

const source = canceltoken.source(); //建立標誌請求的源物件

axios.get('/user/12345', ).catch(function (thrown) else

});axios.post('/user/12345', , )

source.cancel('operation canceled by the user.'); // 取消導致之前的請求

斷點續傳

場景:檔案上傳被中斷,需要續傳

及時獲取到已經上傳的部分,儲存已上傳部分:

this.loaded = progressevent.loaded

剪裁檔案:

this.loaded = progressevent.loaded   //  獲取已經上傳的部分

var filedata = this.file.slice(this.loaded +1 , this.file,size) //裁剪檔案

var fd = new formdata();

const canceltoken = axios.canceltoken;

const source = canceltoken.source();

this.axios.post('url',fd,)

***

應用場景1:在請求發起之前,展示乙個loading,在響應回來之後,關閉乙個loading

send:function());

//響應***

this.$ axios.interceptors.reponse.use(function(res));

this.$ axios.get('url')

.then(res=>

應用場景2:實現乙個類似的cookie機制

伺服器 ----設定set-cookie:*** 儲存起來在響應中完成

在請求之前,本地獲取***,設定***,請求頭

sendajax:function()

this.isshow = true;

return res.config;

}) // 響應***

this.$axios.interceptors.response.use((res)=>

return res.config;

})

知識點總結

1,迴圈中的中斷 continue 跳出此次迴圈,繼續for迴圈 break 跳出當前for迴圈 return 跳出當前方法 2,字串的操作 componentseparatedbystring stringbyreplacingoccurencesofstring withstring iskin...

知識點總結

oncreate onstrat onresume onpause onstop onrestart ondestroy standard 啟動activity都會產生乙個新的activity 預設模式 singletop 啟動activity允許多個,但不允許重疊 singletask 只允許有乙...

知識點總結

function go go 呼叫 go為變數名 setinterval go,1000 1000ms clearinterval 關閉定時器 function abc a,b 2,3 5undefined 未定義 null 空 nan 非數值 string 字串 var a 123 數字型別 va...