最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

vue+axios 前端實(shí)現(xiàn)的常用攔截的代碼示例

 更新時(shí)間:2018年08月23日 11:20:14   作者:風(fēng)雨后見彩虹  
這篇文章主要介紹了vue+axios 前端實(shí)現(xiàn)的常用攔截的代碼示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧

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()

參考地址:vue中axios解決跨域問題和攔截器使用

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

绥棱县| 红安县| 武川县| 沧源| 准格尔旗| 金华市| 仪征市| 门源| 延寿县| 云林县| 铜鼓县| 固镇县| 株洲市| 镇江市| 清苑县| 苏尼特左旗| 东兰县| 东莞市| 双柏县| 遂溪县| 鄂伦春自治旗| 松原市| 三明市| 玉林市| 台前县| 朝阳县| 蕉岭县| 余庆县| 溧水县| 石家庄市| 仁布县| 扎鲁特旗| 沁水县| 固始县| 彭州市| 巴南区| 十堰市| 绥江县| 霍城县| 张北县| 巴青县|