Vue項(xiàng)目中如何封裝axios(統(tǒng)一管理http請(qǐng)求)
在使用Vue.js框架開(kāi)發(fā)前端項(xiàng)目時(shí),會(huì)經(jīng)常發(fā)送ajax請(qǐng)求服務(wù)端接口,在開(kāi)發(fā)過(guò)程中,需要對(duì)axios進(jìn)一步封裝,方便在項(xiàng)目中的使用。
2、Vue項(xiàng)目結(jié)構(gòu)在本地創(chuàng)建Vue項(xiàng)目,目錄結(jié)構(gòu)如下:
- public 靜態(tài)資源文件 - src |- assets 靜態(tài)資源目錄 |- components 公共組件目錄 |- http axios封裝目錄 |- router 路由管理目錄 |- store 狀態(tài)管理目錄 |- views 視圖組件目錄 |- App.vue 根組件 |- main.js 入口文件 - package.json npm配置文件
在Vue項(xiàng)目中創(chuàng)建 http目錄 作為axios的管理目錄,在 http目錄 下兩個(gè)文件,分別是
/http/index.js 封裝axios方法的文件 /http/api.js 統(tǒng)一管理接口的文件 3、代碼示例/http/api.js文件代碼如下:
export default { ’users_add’: ’/users/add’, ’users_find’: ’/users/find’, ’users_update’: ’/users/update’, ’users_delete’: ’/users/delete’}
/http/index.js文件代碼如下:
import axios from ’axios’import api from ’./api’//創(chuàng)建axios實(shí)例對(duì)象let instance = axios.create({ baseURL: ’http://localhost:3000’, //服務(wù)器地址 timeout: 5000 //默認(rèn)超時(shí)時(shí)長(zhǎng)})//請(qǐng)求攔截器instance.interceptors.request.use(config=>{ //此處編寫請(qǐng)求攔截的代碼,一般用于彈出加載窗口 console.log(’正在請(qǐng)求……’) return config},err=>{ console.error(’請(qǐng)求失敗’,err)})//響應(yīng)攔截器instance.interceptors.response.use(res=>{ //此處對(duì)響應(yīng)數(shù)據(jù)做處理 console.log(’請(qǐng)求成功!’) return res //該返回對(duì)象會(huì)傳到請(qǐng)求方法的響應(yīng)對(duì)象中},err=>{ // 響應(yīng)錯(cuò)誤處理 console.log(’響應(yīng)失敗!’,err) // return Promise.reject(err);})//封裝axios請(qǐng)求方法,參數(shù)為配置對(duì)象//option = {method,url,params} method為請(qǐng)求方法,url為請(qǐng)求接口,params為請(qǐng)求參數(shù)async function http(option = {}) { let result = null if(option.method === ’get’ || option.method === ’delete’){ //處理get、delete請(qǐng)求await instance[option.method](api[option.url],{params: option.params} ).then(res=>{ result = res.data}).catch(err=>{ result = err}) }else if(option.method === ’post’ || option.method === ’put’){ //處理post、put請(qǐng)求await instance[option.method](api[option.url],option.params ).then(res=>{ result = res.data}).catch(err=>{ result = err}) } return result}export default http
在main.js入口文件中引入封裝好的 /http/index.js 文件,示例代碼如下:
import Vue from ’vue’import App from ’./App.vue’import router from ’./router’import store from ’./store’import http from ’./http’Vue.config.productionTip = falseVue.prototype.$http = httpVue.use(Elementui)new Vue({ router, store, render: h => h(App)}).$mount(’#app’)
在App.vue根組件中測(cè)試axios請(qǐng)求,示例代碼如下:
<template> <div> <button @click='getDate'>發(fā)送請(qǐng)求</el-button> </div></template><script>export default { methods: { getDate(){ this.$http({method: ’get’,url: ’users_find’ }).then(res=>{console.log(res) }) } }}</script>
這里需要有 http://localhost:3000/users/find 接口,不然請(qǐng)求會(huì)失敗!
4、效果演示啟動(dòng)Vue項(xiàng)目,在瀏覽器中訪問(wèn)Vue項(xiàng)目的地址,我的地址是 http://localhost:8080,點(diǎn)擊按鈕發(fā)送請(qǐng)求,獲取的結(jié)果如下圖所示。
到此,在Vue項(xiàng)目中就完成了簡(jiǎn)單的axios封裝,你也可以根據(jù)自己的實(shí)際需求對(duì)axios進(jìn)行封裝,本文只是提供參考。
到此這篇關(guān)于Vue項(xiàng)目中如何封裝axios(統(tǒng)一管理http請(qǐng)求)的文章就介紹到這了,更多相關(guān)Vue封裝axios管理http請(qǐng)求內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. HTTP協(xié)議常用的請(qǐng)求頭和響應(yīng)頭響應(yīng)詳解說(shuō)明(學(xué)習(xí))2. idea設(shè)置提示不區(qū)分大小寫的方法3. .NET SkiaSharp 生成二維碼驗(yàn)證碼及指定區(qū)域截取方法實(shí)現(xiàn)4. ASP.NET MVC通過(guò)勾選checkbox更改select的內(nèi)容5. css代碼優(yōu)化的12個(gè)技巧6. IntelliJ IDEA創(chuàng)建web項(xiàng)目的方法7. 原生JS實(shí)現(xiàn)記憶翻牌游戲8. Django使用HTTP協(xié)議向服務(wù)器傳參方式小結(jié)9. CentOS郵件服務(wù)器搭建系列—— POP / IMAP 服務(wù)器的構(gòu)建( Dovecot )10. django創(chuàng)建css文件夾的具體方法
