Vue簡(jiǎn)單封裝axios網(wǎng)絡(luò)請(qǐng)求的方法
Vue簡(jiǎn)單封裝axios網(wǎng)絡(luò)請(qǐng)求

一、utils下的httpUtils.js:
import axios from 'axios';
import querystring from 'querystring';
const errorHandler = (status, info) => {
switch(status){
case 400:
console.log("400 - 語(yǔ)義錯(cuò)誤");
break;
case 401:
console.log("401 - 服務(wù)器認(rèn)證失敗");
break;
case 403:
console.log("403 - 服務(wù)器拒絕訪問(wèn)");
break;
case 404:
console.log("404 - 地址錯(cuò)誤");
break;
case 500:
console.log("500 - 服務(wù)器遇到意外");
break;
case 502:
console.log("502 - 服務(wù)器無(wú)響應(yīng)");
break;
default:
console.log(info);
break;
}
}
// 創(chuàng)建axios實(shí)例
const instance = axios.create({
// 放置網(wǎng)絡(luò)請(qǐng)求的公共參數(shù)
timeout:5000, // 5s后超時(shí)
})
// 攔截器最常用
// 1、發(fā)送請(qǐng)求之前
instance.interceptors.request.use(
config =>{
if (config.method === 'post'){
config.data = querystring.stringify(config.data)
}
// config中存在網(wǎng)絡(luò)請(qǐng)求的所有信息
return config;
},
error =>{
return Promise.reject(error)
}
)
// 2、接收響應(yīng)后
instance.interceptors.response.use(
response => {
// 三目運(yùn)算根據(jù)狀態(tài)碼來(lái)判斷接收數(shù)據(jù)還是拒絕數(shù)據(jù)
return response.status === 200 ? Promise.resolve(response):Promise.reject(response)
},
error=>{
const { response } = error;
// 自定義方法來(lái)輸出異常信息
errorHandler(response.status,response.info)
}
)
// 導(dǎo)出
export default instance;
二、/api下的path.js:
const base = {
// 公共路徑
baseUrl : "http://iwenwiki.com",
chengpin : "/api/blueberrypai/getChengpinDetails.php",
login : "/api/blueberrypai/login.php"
}
export default base;三、/api下的index.js:
import axios from "../utils/httpUtils";
import path from "./path"
const api = {
// 成品地址
getChengpin(){
return axios.get(path.baseUrl+path.chengpin)
}
}
export default api;四、組件中引入并請(qǐng)求:
<template>
<div>
<p>{{ msg }}</p>
</div>
</template>
<script>
// import axios from 'axios';
// import QueryString from 'qs';
import api from "../api/index"
export default {
name: 'HelloWorld',
data(){
return{
msg:"111",
}
},
mounted(){
//Fetch API 基本用法
// fetch('http://iwenwiki.com/api/blueberrypai/getChengpinDetails.php').then(function(data){
// // text()方法屬于fetchAPI的一部分,它返回一個(gè)Promise實(shí)例對(duì)象,用于獲取后臺(tái)返回的數(shù)據(jù)
// return data.json();
// }).then(function(data){
// console.log(data.chengpinDetails[0].title);
// })
// get
// axios({
// method:"get",
// url:"http://iwenwiki.com/api/blueberrypai/getChengpinDetails.php",
// }).then(res=>{
// this.msg=res.data.chengpinDetails[0].title
// })
// post
// axios({
// method:"post",
// url:"http://iwenwiki.com/api/blueberrypai/login.php",
// data: QueryString.stringify({
// user_id: "iwen@qq.com",
// password: "iwen123",
// verification_code: "crfvw"
// })
// }).then(res=>{
// this.msg=res.data
// })
// 第二種get方法
// axios.get('http://iwenwiki.com/api/blueberrypai/getChengpinDetails.php').then(res=>{
// this.msg=res.data.chengpinDetails[0].title
// })
// 第二種post方法
// this.$axios.post('http://iwenwiki.com/api/blueberrypai/login.php',QueryString.stringify({
// user_id: "iwen@qq.com",
// password: "iwen123",
// verification_code: "crfvw"
// })).then(res=>{
// this.msg=res.data.success
// })
api.getChengpin().then(res=>{
console.log(res.data)
})
}
}
</script>
<style scoped>
</style>
查看效果:

請(qǐng)求成功
到此這篇關(guān)于Vue簡(jiǎn)單封裝axios網(wǎng)絡(luò)請(qǐng)求的文章就介紹到這了,更多相關(guān)Vue封裝axios網(wǎng)絡(luò)請(qǐng)求內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Spring Boot + Vue 前后端分離開(kāi)發(fā)之前端網(wǎng)絡(luò)請(qǐng)求封裝與配置
- Vue網(wǎng)絡(luò)請(qǐng)求的三種實(shí)現(xiàn)方式介紹
- Vue3如何使用axios發(fā)起網(wǎng)絡(luò)請(qǐng)求
- vue網(wǎng)絡(luò)請(qǐng)求方案原生網(wǎng)絡(luò)請(qǐng)求和js網(wǎng)絡(luò)請(qǐng)求庫(kù)
- 在小程序/mpvue中使用flyio發(fā)起網(wǎng)絡(luò)請(qǐng)求的方法
- 淺談Vue網(wǎng)絡(luò)請(qǐng)求之interceptors實(shí)際應(yīng)用
- Vue項(xiàng)目的網(wǎng)絡(luò)請(qǐng)求代理到封裝步驟詳解
相關(guān)文章
Vue.js學(xué)習(xí)筆記之 helloworld
vue是法語(yǔ)中視圖的意思,Vue.js是一個(gè)輕巧、高性能、可組件化的MVVM庫(kù),同時(shí)擁有非常容易上手的API。有需要的小伙伴可以參考下2016-08-08
Vue 項(xiàng)目部署到服務(wù)器的問(wèn)題解決方法
本篇文章主要介紹了Vue 項(xiàng)目部署到服務(wù)器的問(wèn)題解決方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-12-12
vue3使用localStorage實(shí)現(xiàn)登錄注冊(cè)功能實(shí)例
這篇文章主要給大家介紹了關(guān)于vue3使用localStorage實(shí)現(xiàn)登錄注冊(cè)功能的相關(guān)資料, localStorage這個(gè)特性主要是用來(lái)作為本地存儲(chǔ)來(lái)使用的,解決了cookie存儲(chǔ)空間不足的問(wèn)題,需要的朋友可以參考下2023-06-06
vscode配置@路徑提示方式,并解決vue文件內(nèi)失效的問(wèn)題
這篇文章主要介紹了vscode配置@路徑提示方式,并解決vue文件內(nèi)失效的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-10-10
Vue使用輪詢(xún)定時(shí)發(fā)送請(qǐng)求代碼
這篇文章主要介紹了Vue使用輪詢(xún)定時(shí)發(fā)送請(qǐng)求代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-08-08
Vue-Ant Design Vue-普通及自定義校驗(yàn)實(shí)例
這篇文章主要介紹了Vue-Ant Design Vue-普通及自定義校驗(yàn)實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-10-10
Vue3通過(guò)ref操作Dom元素及hooks的使用方法
這篇文章主要介紹了Vue3通過(guò)ref操作Dom元素及hooks的使用方法,需要的朋友可以參考下2023-01-01

