vue項目使用axios封裝request請求的過程
一、封裝axios
1.src 目錄中新建utils文件夾
2.utils文件中建立request.js文件

request.js文件的內(nèi)容
<1> 導(dǎo)入axios
<2> 我們可以聲明一個新的常量axios 我們可以配置一些基礎(chǔ) 公共的路徑配置 比如說baseURL timeout請求失敗超時報錯 withcookies…之類的東西
<3> 設(shè)置請求攔截
新的常量axios service.攔截器.請求.使用===》 里頭有兩個參數(shù) 一個成功的回調(diào)函數(shù) 一個失敗的回調(diào)函數(shù)
<4> 設(shè)置響應(yīng)攔截
<5> 導(dǎo)出 導(dǎo)出這個模塊
完整代碼:
import axios from 'axios'
import { Notification } from 'element-ui'
// 創(chuàng)建axios實例
const service = axios.create({
// baseURL: 'http://192.168.1.69:5000',
baseURL: process.env.API_ROOT,
timeout: 80000, // 請求超時時間
withCredentials: true,
// crossDomain: true
})
// request攔截器
service.interceptors.request.use(
config => {
if (getToken()) {
config.headers['Authorization'] = getToken() // 讓每個請求攜帶自定義token 請根據(jù)實際情況自行修改
}
var lang = localStorage.getItem('lang')//因為項目中使用到了i18n國際化語言配置,請根據(jù)實際情況自行修改
if (!lang) {
lang = 'zh_CN'
}
config.headers['Accept-Language'] = lang.replace(/_/g, '-')
config.headers['Content-Type'] = 'application/json'
return config
},
error => {
Promise.reject(error)
}
)
// response 攔截器
service.interceptors.response.use(
response => {
return response.data
},
error => {
// 兼容blob下載出錯json提示
if (error.response.data instanceof Blob && error.response.data.type.toLowerCase().indexOf('json') !== -1) {
const reader = new FileReader()
reader.readAsText(error.response.data, 'utf-8')
reader.onload = function (e) {
const errorMsg = JSON.parse(reader.result).message
Notification.error({
title: errorMsg,
duration: 5000
})
}
} else {
let code = 0
try {
code = error.response.data.status
} catch (e) {
if (error.toString().indexOf('Error: timeout') !== -1) {
Notification.error({
title: '網(wǎng)絡(luò)請求超時',
duration: 5000
})
return Promise.reject(error)
}
}
if (code) {
if (code === 401) {
store.dispatch('LogOut').then(() => {
// 用戶登錄界面提示
Cookies.set('point', 401)
location.reload()
})
} else if (code === 403) {
router.push({ path: '/401' })
} else {
const errorMsg = error.response.data.message
if (errorMsg !== undefined) {
Notification.error({
title: errorMsg,
duration: 0
})
}
}
} else {
Notification.error({
title: '接口請求失敗',
duration: 5000
})
}
}
return Promise.reject(error)
}
)
export default service
二、封裝api 函數(shù)

1.先導(dǎo)入封裝好的新的axios
2.然后我們可以封裝一些接口函數(shù) 比如說 登錄的 注冊的 首頁的 分類的 輪播的 //但是要確認(rèn)參數(shù)傳的是get還是post請求
tips://比如說以后我們要維護(hù)封裝好的接口 這樣封裝好后我們就不需要去組件里一個一個去找,直接來這個文件修改即可
//組件化開模塊化發(fā)或者開發(fā) 它們都有一個原則
//高聚合 低耦合
//高聚合就是 一個組件的業(yè)務(wù)一定要聚合在一起 一個組件的業(yè)務(wù)越集中越好
//低耦合就是 組件和組件之間的耦合度一定要低 意思就是兩個組件之間的牽連越少越好

三、頁面中使用
<script>標(biāo)簽中引入
import {
getSetbrate,randomtest,autocalibrate,lightCheck,writeserial} from "@/api/mAdmin";方法中調(diào)用

// 設(shè)置閾值 獲取閾值
onManualSend() {
let data = {
command: this.command,
};
writeserial(data).then((res) => {
this.manualText = res.msg;
});
},到此這篇關(guān)于vue項目 使用axios封裝request請求的文章就介紹到這了,更多相關(guān)vue axios封裝request請求內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue.js中extend選項和delimiters選項的比較
這篇文章主要介紹了Vue.js中extend選項和delimiters選項的比較的相關(guān)資料,需要的朋友可以參考下2017-07-07
Vue的transition-group與Virtual Dom Diff算法的使用
這篇文章主要介紹了Vue的transition-group與Virtual Dom Diff算法的使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12
vue實現(xiàn)靜態(tài)頁面點(diǎn)贊和取消點(diǎn)贊功能
這篇文章主要為大家詳細(xì)介紹了vue實現(xiàn)靜態(tài)頁面點(diǎn)贊和取消點(diǎn)贊的功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-02-02

