Vue簡單封裝axios網(wǎng)絡(luò)請求的方法
更新時間:2022年11月15日 14:33:11 作者:隨筆都是學(xué)習(xí)筆記
這篇文章主要介紹了Vue簡單封裝axios網(wǎng)絡(luò)請求,本文通過示例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,對Vue封裝axios網(wǎng)絡(luò)請求相關(guān)知識感興趣的朋友一起看看吧
Vue簡單封裝axios網(wǎng)絡(luò)請求

一、utils下的httpUtils.js:
import axios from 'axios';
import querystring from 'querystring';
const errorHandler = (status, info) => {
switch(status){
case 400:
console.log("400 - 語義錯誤");
break;
case 401:
console.log("401 - 服務(wù)器認(rèn)證失敗");
break;
case 403:
console.log("403 - 服務(wù)器拒絕訪問");
break;
case 404:
console.log("404 - 地址錯誤");
break;
case 500:
console.log("500 - 服務(wù)器遇到意外");
break;
case 502:
console.log("502 - 服務(wù)器無響應(yīng)");
break;
default:
console.log(info);
break;
}
}
// 創(chuàng)建axios實(shí)例
const instance = axios.create({
// 放置網(wǎng)絡(luò)請求的公共參數(shù)
timeout:5000, // 5s后超時
})
// 攔截器最常用
// 1、發(fā)送請求之前
instance.interceptors.request.use(
config =>{
if (config.method === 'post'){
config.data = querystring.stringify(config.data)
}
// config中存在網(wǎng)絡(luò)請求的所有信息
return config;
},
error =>{
return Promise.reject(error)
}
)
// 2、接收響應(yīng)后
instance.interceptors.response.use(
response => {
// 三目運(yùn)算根據(jù)狀態(tài)碼來判斷接收數(shù)據(jù)還是拒絕數(shù)據(jù)
return response.status === 200 ? Promise.resolve(response):Promise.reject(response)
},
error=>{
const { response } = error;
// 自定義方法來輸出異常信息
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;四、組件中引入并請求:
<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的一部分,它返回一個Promise實(shí)例對象,用于獲取后臺返回的數(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>
查看效果:

請求成功
到此這篇關(guān)于Vue簡單封裝axios網(wǎng)絡(luò)請求的文章就介紹到這了,更多相關(guān)Vue封裝axios網(wǎng)絡(luò)請求內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:
- Spring Boot + Vue 前后端分離開發(fā)之前端網(wǎng)絡(luò)請求封裝與配置
- Vue網(wǎng)絡(luò)請求的三種實(shí)現(xiàn)方式介紹
- Vue3如何使用axios發(fā)起網(wǎng)絡(luò)請求
- vue網(wǎng)絡(luò)請求方案原生網(wǎng)絡(luò)請求和js網(wǎng)絡(luò)請求庫
- 在小程序/mpvue中使用flyio發(fā)起網(wǎng)絡(luò)請求的方法
- 淺談Vue網(wǎng)絡(luò)請求之interceptors實(shí)際應(yīng)用
- Vue項目的網(wǎng)絡(luò)請求代理到封裝步驟詳解
相關(guān)文章
Vue2.X 通過AJAX動態(tài)更新數(shù)據(jù)
這篇文章主要介紹了Vue2.X 通過AJAX動態(tài)更新數(shù)據(jù)的方法,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2018-07-07
Vue Element前端應(yīng)用開發(fā)之動態(tài)菜單和路由的關(guān)聯(lián)處理
這篇文章主要介紹了Vue Element前端應(yīng)用開發(fā)之動態(tài)菜單和路由的關(guān)聯(lián)處理,對vue感興趣的同學(xué),可以參考下2021-05-05
Vue服務(wù)端渲染實(shí)踐之Web應(yīng)用首屏耗時最優(yōu)化方案
這篇文章主要介紹了Vue服務(wù)端渲染實(shí)踐之Web應(yīng)用首屏耗時最優(yōu)化方案,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03

