vue中axios的二次封裝實(shí)例講解
我們做項(xiàng)目時(shí),雖然axios也可以直接拿來(lái)用,但是對(duì)接口比較零散,不太好進(jìn)行維護(hù),也會(huì)產(chǎn)生大量的重復(fù)代碼,所以我在這對(duì)axios進(jìn)行了統(tǒng)一接口處理
第一步,先在src中的公共文件夾中如utils里新建request.js文件

import axios from 'axios'
import router from '@/router/routers'
import { Notification, MessageBox } from 'element-ui'
import store from '../store'
import { getToken } from '@/utils/auth'
import Config from '@/config'
import {baseUrl} from '@/utils/env'
// 創(chuàng)建axios實(shí)例
const service = axios.create({
baseURL: baseUrl, // api 的 base_url
// baseURL: process.env.BASE_API, // api 的 base_url
timeout: Config.timeout // 請(qǐng)求超時(shí)時(shí)間
})
// request攔截器
service.interceptors.request.use(
config => {
if (getToken()) {
config.headers['Authorization'] = 'Bearer ' + getToken() // 讓每個(gè)請(qǐng)求攜帶自定義token 請(qǐng)根據(jù)實(shí)際情況自行修改
}
config.headers['Content-Type'] = 'application/json'
return config
},
error => {
// Do something with request error
console.log(error) // for debug
Promise.reject(error)
}
)
// response 攔截器
service.interceptors.response.use(
response => {
const code = response.status
console.log(response)
if (code < 200 || code > 300) {
Notification.error({
title: response.message
})
return Promise.reject('error')
} else {
return response.data
}
},
error => {
let code = 0
try {
code = error.response.data.status
} catch (e) {
if (error.toString().indexOf('Error: timeout') !== -1) {
Notification.error({
title: '網(wǎng)絡(luò)請(qǐng)求超時(shí)',
duration: 2500
})
return Promise.reject(error)
}
if (error.toString().indexOf('Error: Network Error') !== -1) {
Notification.error({
title: '網(wǎng)絡(luò)請(qǐng)求錯(cuò)誤',
duration: 2500
})
return Promise.reject(error)
}
}
if (code === 401) {
MessageBox.confirm(
'登錄狀態(tài)已過(guò)期,您可以繼續(xù)留在該頁(yè)面,或者重新登錄',
'系統(tǒng)提示',
{
confirmButtonText: '重新登錄',
cancelButtonText: '取消',
type: 'warning'
}
).then(() => {
store.dispatch('LogOut').then(() => {
location.reload() // 為了重新實(shí)例化vue-router對(duì)象 避免bug
})
})
} else if (code === 403) {
router.push({ path: '/401' })
} else {
const errorMsg = error.response.data.message
if (errorMsg !== undefined) {
Notification.error({
title: errorMsg,
duration: 2500
})
}
}
return Promise.reject(error)
}
)
export default service
代碼解讀:


將接口統(tǒng)一放到單獨(dú)的文件中如我的

引入request.js

第三步,
在頁(yè)面使用


好了,這就是axios的二次封裝
以上就是關(guān)于vue中axios的二次封裝的全部知識(shí)點(diǎn)內(nèi)容,感謝大家的學(xué)習(xí)和對(duì)腳本之家的支持。
相關(guān)文章
vue實(shí)現(xiàn)路由跳轉(zhuǎn)動(dòng)態(tài)title標(biāo)題信息
這篇文章主要介紹了vue實(shí)現(xiàn)路由跳轉(zhuǎn)動(dòng)態(tài)title標(biāo)題信息,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06
vue項(xiàng)目實(shí)現(xiàn)通過(guò)ip地址訪問(wèn)和localhost訪問(wèn)方式
這篇文章主要介紹了vue項(xiàng)目實(shí)現(xiàn)通過(guò)ip地址訪問(wèn)和localhost訪問(wèn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-09-09
Vue中瀑布流布局與圖片加載優(yōu)化的實(shí)現(xiàn)
本文主要介紹了Vue中瀑布流布局與圖片加載優(yōu)化的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06
vue-cli3在main.js中console.log()會(huì)報(bào)錯(cuò)的解決
這篇文章主要介紹了vue-cli3在main.js中console.log()會(huì)報(bào)錯(cuò)的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04
使用watch監(jiān)聽(tīng)路由變化和watch監(jiān)聽(tīng)對(duì)象的實(shí)例
下面小編就為大家分享一篇使用watch監(jiān)聽(tīng)路由變化和watch監(jiān)聽(tīng)對(duì)象的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-02-02
vue實(shí)現(xiàn)接口封裝的實(shí)現(xiàn)示例
本文主要介紹了vue實(shí)現(xiàn)接口封裝的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-11-11

