vue-resource 攔截器interceptors使用詳解
前言
攔截器-interceptor
在現(xiàn)代的一些前端框架上,攔截器基本上是很基礎(chǔ)但很重要的一環(huán),比如Angular原生就支持?jǐn)r截器配置,VUE的Axios模塊也給我們提供了攔截器配置,那么攔截器到底是什么,它有什么用?
攔截器能幫助我們解決的
- 添加統(tǒng)一的request的參數(shù)
- 比如header中加入X-Requested-With,比如客戶端需要實(shí)現(xiàn)sign和token的驗(yàn)證機(jī)制,比如你可以寫(xiě)$http.get(‘/files', params),攔截器幫你拼接成 http://www.xxxx.com/1/files 這樣的請(qǐng)求地址
- 處理統(tǒng)一的responseError
- 比如重連機(jī)制,拿到error.code錯(cuò)誤碼重連,比如token過(guò)期,重新拿到token再次send request
- 比如統(tǒng)一報(bào)錯(cuò)信息,給所有返回的404來(lái)個(gè)提示也會(huì)很酷
在vue項(xiàng)目使用vue-resource實(shí)現(xiàn)異步加載的過(guò)程中,需要在任何一個(gè)頁(yè)面任何一次http請(qǐng)求過(guò)程中,增加對(duì)token過(guò)期的判斷,如果token已過(guò)期,需要跳轉(zhuǎn)至登錄頁(yè)面。如果要在每個(gè)頁(yè)面中的http請(qǐng)求操作中添加一次判斷,那將會(huì)是一個(gè)非常大的修改工作量。那么vue-resource是否存在一個(gè)對(duì)于任何一次請(qǐng)求響應(yīng)捕獲的的公共回調(diào)函數(shù)呢?答案是有的!
vue-resource的interceptors攔截器的作用正是解決此需求的妙方。在每次http的請(qǐng)求響應(yīng)之后,如果設(shè)置了攔截器,會(huì)優(yōu)先執(zhí)行攔截器函數(shù),獲取響應(yīng)體,然后才會(huì)決定是否把response返回給then進(jìn)行接收。那么我們可以在這個(gè)攔截器里邊添加對(duì)響應(yīng)狀態(tài)碼的判斷,來(lái)決定是跳轉(zhuǎn)到登錄頁(yè)面還是留在當(dāng)前頁(yè)面繼續(xù)獲取數(shù)據(jù)。

安裝與引用
NPM: npm install vue-resource --save-dev
/*引入Vue框架*/ import Vue from 'vue' /*引入資源請(qǐng)求插件*/ import VueResource from 'vue-resource' /*使用VueResource插件*/ Vue.use(VueResource)
下邊代碼添加在main.js中
Vue.http.interceptors.push((request, next) => {
console.log(this)//此處this為請(qǐng)求所在頁(yè)面的Vue實(shí)例
// modify request
request.method = 'POST';//在請(qǐng)求之前可以進(jìn)行一些預(yù)處理和配置
// continue to next interceptor
next((response) => {//在響應(yīng)之后傳給then之前對(duì)response進(jìn)行修改和邏輯判斷。對(duì)于token時(shí)候已過(guò)期的判斷,就添加在此處,頁(yè)面中任何一次http請(qǐng)求都會(huì)先調(diào)用此處方法
response.body = '...';
return response;
});
});
攔截器實(shí)例
(1)為請(qǐng)求添加loading效果
通過(guò)inteceptor,我們可以為所有的請(qǐng)求處理加一個(gè)loading:請(qǐng)求發(fā)送前顯示loading,接收響應(yīng)后隱藏loading。
具體步驟如下:
//1、添加一個(gè)loading組件
<template id='loading-template'>
<div class='loading-overlay'></div>
</template>
//2、將loading組件作為另外一個(gè)Vue實(shí)例的子組件
var help = new Vue({
el: '#help',
data: {
showLoading: false
},
components: {
'loading': {
template: '#loading-template',
}
}
})
//3、將該Vue實(shí)例掛載到某個(gè)HTML元素
<div id='help'>
<loading v-show='showLoading'></loading>
</div>
//4、添加inteceptor
Vue.http.interceptors.push((request, next) => {
loading.show = true;
next((response) => {
loading.show = false;
return response;
});
});
但是,當(dāng)用戶在畫(huà)面上停留時(shí)間太久時(shí),視圖數(shù)據(jù)可能已經(jīng)不是最新的了,這時(shí)如果用戶刪除或修改某一條數(shù)據(jù),如果這條數(shù)據(jù)已經(jīng)被其他用戶刪除了,服務(wù)器會(huì)反饋一個(gè)404的錯(cuò)誤,但由于我們的put和delete請(qǐng)求沒(méi)有處理errorCallback,所以用戶是不知道他的操作是成功還是失敗了。這個(gè)問(wèn)題,同樣也可以通過(guò)inteceptor解決。
(2)為請(qǐng)求添加共同的錯(cuò)誤處理方法
//1、繼續(xù)沿用上面的loading組件,在#help元素下加一個(gè)對(duì)話框
<div id='help'>
<loading v-show='showLoading' ></loading>
<modal-dialog :show='showDialog'>
<header class='dialog-header' slot='header'>
<h3 class='dialog-title'>Server Error</h3>
</header>
<div class='dialog-body' slot='body'>
<p class='error'>Oops,server has got some errors, error code: {{errorCode}}.</p>
</div>
</modal-dialog>
</div>
//2、給help實(shí)例的data選項(xiàng)添加兩個(gè)屬性
var help = new Vue({
el: '#help',
data: {
showLoading: false,
showDialog: false,
errorCode: ''
},
components: {
'loading': {
template: '#loading-template',
}
}
})
//3、修改inteceptor
Vue.http.interceptors.push((request, next) => {
help.showLoading = true;
next((response) => {
if(!response.ok){
help.errorCode = response.status;
help.showDialog = true;
}
help.showLoading = false;
return response;
});
});
到此這篇關(guān)于vue-resource 攔截器interceptors使用詳解的文章就介紹到這了,更多相關(guān)vue-resource攔截器內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue在H5 項(xiàng)目中使用融云進(jìn)行實(shí)時(shí)個(gè)人單聊通訊
這篇文章主要介紹了Vue在H5 項(xiàng)目中使用融云進(jìn)行實(shí)時(shí)個(gè)人單聊通訊,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-12-12
vue中多個(gè)倒計(jì)時(shí)實(shí)現(xiàn)代碼實(shí)例
這篇文章主要介紹了vue中多個(gè)倒計(jì)時(shí)實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03
express+vue+mongodb+session 實(shí)現(xiàn)注冊(cè)登錄功能
這篇文章主要介紹了express+vue+mongodb+session 實(shí)現(xiàn)注冊(cè)登錄,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-12-12
Vue實(shí)現(xiàn)彈出框點(diǎn)擊空白頁(yè)彈框消失效果
這篇文章主要介紹了VUE實(shí)現(xiàn)彈出框點(diǎn)擊空白頁(yè)彈框消失,實(shí)現(xiàn)方法可以在Vue中實(shí)現(xiàn)彈出框然后通過(guò)點(diǎn)擊空白頁(yè)面來(lái)讓彈窗隱藏,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2023-12-12
Vuex中g(shù)etters和actions的使用補(bǔ)充說(shuō)明
這篇文章主要介紹了在Vuex中關(guān)于getters和actions使用的補(bǔ)充作了簡(jiǎn)要說(shuō)明,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2021-09-09
Vite創(chuàng)建Vue3項(xiàng)目及Vue3使用jsx詳解
vite是新一代的前端構(gòu)建工具,下面這篇文章主要給大家介紹了關(guān)于Vite創(chuàng)建Vue3項(xiàng)目以及Vue3使用jsx的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-08-08

