溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

怎么用vue實現(xiàn)axios的二次封裝

發(fā)布時間:2022-05-06 13:43:46 來源:億速云 閱讀:162 作者:iii 欄目:大數(shù)據(jù)

這篇文章主要介紹了怎么用vue實現(xiàn)axios的二次封裝的相關(guān)知識,內(nèi)容詳細易懂,操作簡單快捷,具有一定借鑒價值,相信大家閱讀完這篇怎么用vue實現(xiàn)axios的二次封裝文章都會有所收獲,下面我們一起來看看吧。

定義公共參數(shù)與引入組件:

import axios from 'axios'
import qs from 'qs'
 
axios.interceptors.request.use(config => {
  //顯示loading
 return config
}, error => {
 return Promise.reject(error)
})
 
 
axios.interceptors.response.use(response => {
 return response
}, error => {
 return Promise.resolve(error.response)
})
 
function errorState(response) {
  //隱藏loading
 console.log(response)
 // 如果http狀態(tài)碼正常,則直接返回數(shù)據(jù)
 if (response && (response.status === 200 || response.status === 304 || response.status === 400)) {
  return response
   // 如果不需要除了data之外的數(shù)據(jù),可以直接 return response.data
 }else{
  Vue.prototype.$msg.alert.show({
      title: '提示',
      content: '網(wǎng)絡(luò)異常'
  })
 }
 
}
 
function successState(res) {
  //隱藏loading
 //統(tǒng)一判斷后端返回的錯誤碼
 if(res.data.errCode == '000002'){
    Vue.prototype.$msg.alert.show({
      title: '提示',
      content: res.data.errDesc||'網(wǎng)絡(luò)異常',
      onShow () {
      },
      onHide () {
       console.log('確定')
      }
    })
 }else if(res.data.errCode != '000002'&&res.data.errCode != '000000') {
   Vue.prototype.$msg.alert.show({
      title: '提示',
      content: res.data.errDesc||'網(wǎng)絡(luò)異常',
      onShow () {
 
      },
      onHide () {
       console.log('確定')
      }
    })
 }
}
const httpServer = (opts, data) => {
 
  let Public = { //公共參數(shù)
   'srAppid': ""
  }
 
  let httpDefaultOpts = { //http默認配置
     method:opts.method,
     baseURL,
     url: opts.url,
     timeout: 10000,
     params:Object.assign(Public, data),
     data:qs.stringify(Object.assign(Public, data)),
     headers: opts.method=='get'?{
      'X-Requested-With': 'XMLHttpRequest',
      "Accept": "application/json",
      "Content-Type": "application/json; charset=UTF-8"
     }:{
      'X-Requested-With': 'XMLHttpRequest',
      'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
     }
  }
 
  if(opts.method=='get'){
   delete httpDefaultOpts.data
  }else{
   delete httpDefaultOpts.params
  }
  
  let promise = new Promise(function(resolve, reject) {
   axios(httpDefaultOpts).then(
    (res) => {
     successState(res)
     resolve(res)
    }
   ).catch(
    (response) => {
     errorState(response)
     reject(response)
    }
   )
 
  })
 return promise
}
 
export default httpServer

封裝理由:

1、可以和后端商量好錯誤碼在這統(tǒng)一提示統(tǒng)一處理,省去不必要的麻煩

2、如果做接口全報文加解密都可以在此處理

接口統(tǒng)一歸類:

const serviceModule = {
 getLocation: {
  url: ' service/location/transfor',
  method: 'get'
 }
}
const ApiSetting = {...serviceModule }
 
export default ApiSetting

歸類好處:

1、后期接口升級或者接口名更改便于維護

http調(diào)用:

<script>
import http from "../../lib/http.js";
import ApiSetting from "../../lib/ApiSetting.js";
export default {
 created: function() {
  http(ApiSetting.getLocation,{"srChannel": "h6",})
  .then((res)=>{
   console.log(res)
  },(error)=>{
   console.log(error)	
  })
  },
 methods: {
 
 }
}
</script>

關(guān)于“怎么用vue實現(xiàn)axios的二次封裝”這篇文章的內(nèi)容就介紹到這里,感謝各位的閱讀!相信大家對“怎么用vue實現(xiàn)axios的二次封裝”知識都有一定的了解,大家如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI