怎么在vue+axios 前端實(shí)現(xiàn)攔截?很多新手對(duì)此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。
創(chuàng)新互聯(lián)是專業(yè)的鐵山網(wǎng)站建設(shè)公司,鐵山接單;提供網(wǎng)站設(shè)計(jì)、成都做網(wǎng)站,網(wǎng)頁設(shè)計(jì),網(wǎng)站設(shè)計(jì),建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進(jìn)行鐵山網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴(kuò)展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團(tuán)隊(duì),希望更多企業(yè)前來合作!
Axios攔截器配置
main.js
//定義一個(gè)請(qǐng)求攔截器 Axios.interceptors.request.use(function(config){ store.state.isShow=true; //在請(qǐng)求發(fā)出之前進(jìn)行一些操作 return config }) //定義一個(gè)響應(yīng)攔截器 Axios.interceptors.response.use(function(config){ store.state.isShow=false;//在這里對(duì)返回的數(shù)據(jù)進(jìn)行處理 return config })
分別定義一個(gè)請(qǐng)求攔截器(請(qǐng)求開始時(shí)執(zhí)行某些操作)、響應(yīng)攔截器(接受到數(shù)據(jù)后執(zhí)行某些操作),之間分別設(shè)置攔截時(shí)執(zhí)行的操作,改變state內(nèi)isShow的布爾值從而控制loading組件在觸發(fā)請(qǐng)求數(shù)據(jù)開始時(shí)顯示loading,返回?cái)?shù)據(jù)時(shí)隱藏loading
特別注意:這里有一個(gè)語法坑(我可是來來回回踩了不少次)main.js中調(diào)取、操作vuex state中的數(shù)據(jù)不同于組件中的this.$store.state,而是直接store.state 同上面代碼
一、路由攔截使用
router.beforeEach((to, from, next) => { if (to.meta.requireAuth) { // 判斷該路由是否需要登錄權(quán)限 if (store.state.token) { // 通過vuex state獲取當(dāng)前的token是否存在 next(); } else { next({ path: '/login', query: {redirect: to.fullPath} // 將跳轉(zhuǎn)的路由path作為參數(shù),登錄成功后跳轉(zhuǎn)到該路由 }) } } else { next(); } })
二、攔截器使用
要想統(tǒng)一處理所有http請(qǐng)求和響應(yīng),就得用上 axios 的攔截器。通過配置http response inteceptor,當(dāng)后端接口返回401 Unauthorized(未授權(quán)),讓用戶重新登錄。
// http request 攔截器 axios.interceptors.request.use( config => { if (store.state.token) { // 判斷是否存在token,如果存在的話,則每個(gè)http header都加上token config.headers.Authorization = `token ${store.state.token}`; } return config; }, err => { return Promise.reject(err); }); // http response 攔截器 axios.interceptors.response.use( response => { return response; }, error => { if (error.response) { switch (error.response.status) { case 401: // 返回 401 清除token信息并跳轉(zhuǎn)到登錄頁面 store.commit(types.LOGOUT); router.replace({ path: 'login', query: {redirect: router.currentRoute.fullPath} }) } } return Promise.reject(error.response.data) // 返回接口返回的錯(cuò)誤信息 });
三、實(shí)例
/** * http配置 */ // 引入axios以及element ui中的loading和message組件 import axios from 'axios' import { Loading, Message } from 'element-ui' // 超時(shí)時(shí)間 axios.defaults.timeout = 5000 // http請(qǐng)求攔截器 var loadinginstace axios.interceptors.request.use(config => { // element ui Loading方法 loadinginstace = Loading.service({ fullscreen: true }) return config }, error => { loadinginstace.close() Message.error({ message: '加載超時(shí)' }) return Promise.reject(error) }) // http響應(yīng)攔截器 axios.interceptors.response.use(data => {// 響應(yīng)成功關(guān)閉loading loadinginstace.close() return data }, error => { loadinginstace.close() Message.error({ message: '加載失敗' }) return Promise.reject(error) }) export default axios
如果你是使用了vux,那么在main.js這樣使用:
Vue.prototype.$http.interceptors.response.use()
看完上述內(nèi)容是否對(duì)您有幫助呢?如果還想對(duì)相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對(duì)創(chuàng)新互聯(lián)的支持。
網(wǎng)頁標(biāo)題:怎么在vue+axios前端實(shí)現(xiàn)攔截
文章轉(zhuǎn)載:http://aaarwkj.com/article6/pjdgog.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供營(yíng)銷型網(wǎng)站建設(shè)、商城網(wǎng)站、網(wǎng)站策劃、標(biāo)簽優(yōu)化、App開發(fā)、網(wǎng)站內(nèi)鏈
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)