Vue项目中封装axios 统一管理http请求

超、凢脫俗 2023-01-15 04:56 291阅读 0赞

1、需求说明

在使用Vue.js框架开发前端项目时,会经常发送ajax请求服务端接口,在开发过程中,需要对axios进一步封装,方便在项目中的使用。

2、Vue项目结构

在本地创建Vue项目,目录结构如下:

  1. - public 静态资源文件
  2. - src
  3. |- assets 静态资源目录
  4. |- components 公共组件目录
  5. |- http axios封装目录
  6. |- router 路由管理目录
  7. |- store 状态管理目录
  8. |- views 视图组件目录
  9. |- App.vue 根组件
  10. |- main.js 入口文件
  11. - package.json npm配置文件

在Vue项目中创建 http目录 作为axios的管理目录,在 http目录 下两个文件,分别是

  • /http/index.js 封装axios方法的文件
  • /http/api.js 统一管理接口的文件

3、代码示例

/http/api.js文件代码如下:

  1. export default {
  2. 'users_add': '/users/add',
  3. 'users_find': '/users/find',
  4. 'users_update': '/users/update',
  5. 'users_delete': '/users/delete'
  6. }

/http/index.js文件代码如下:

  1. import axios from 'axios'
  2. import api from './api'
  3. //创建axios实例对象
  4. let instance = axios.create({
  5. baseURL: 'http://localhost:3000', //服务器地址
  6. timeout: 5000 //默认超时时长
  7. })
  8. //请求拦截器
  9. instance.interceptors.request.use(config=>{
  10. //此处编写请求拦截的代码,一般用于弹出加载窗口
  11. console.log('正在请求……')
  12. return config
  13. },err=>{
  14. console.error('请求失败',err)
  15. })
  16. //响应拦截器
  17. instance.interceptors.response.use(res=>{
  18. //此处对响应数据做处理
  19. console.log('请求成功!')
  20. return res //该返回对象会传到请求方法的响应对象中
  21. },err=>{
  22. // 响应错误处理
  23. console.log('响应失败!',err)
  24. // return Promise.reject(err);
  25. })
  26. //封装axios请求方法,参数为配置对象
  27. //option = {method,url,params} method为请求方法,url为请求接口,params为请求参数
  28. async function http(option = { }) {
  29. let result = null
  30. if(option.method === 'get' || option.method === 'delete'){
  31. //处理get、delete请求
  32. await instance[option.method](
  33. api[option.url],
  34. { params: option.params}
  35. ).then(res=>{
  36. result = res.data
  37. }).catch(err=>{
  38. result = err
  39. })
  40. }else if(option.method === 'post' || option.method === 'put'){
  41. //处理post、put请求
  42. await instance[option.method](
  43. api[option.url],
  44. option.params
  45. ).then(res=>{
  46. result = res.data
  47. }).catch(err=>{
  48. result = err
  49. })
  50. }
  51. return result
  52. }
  53. export default http

在main.js入口文件中引入封装好的 /http/index.js 文件,示例代码如下:

  1. import Vue from 'vue'
  2. import App from './App.vue'
  3. import router from './router'
  4. import store from './store'
  5. import http from './http'
  6. Vue.config.productionTip = false
  7. Vue.prototype.$http = http
  8. Vue.use(Elementui)
  9. new Vue({
  10. router,
  11. store,
  12. render: h => h(App)
  13. }).$mount('#app')

在App.vue根组件中测试axios请求,示例代码如下:

  1. <template>
  2. <div>
  3. <button @click="getDate">发送请求</el-button>
  4. </div>
  5. </template>
  6. <script>
  7. export default {
  8. methods: {
  9. getDate(){
  10. this.$http({
  11. method: 'get',
  12. url: 'users_find'
  13. }).then(res=>{
  14. console.log(res)
  15. })
  16. }
  17. }
  18. }
  19. </script>

这里需要有 http://localhost:3000/users/find 接口,不然请求会失败!

4、效果演示

启动Vue项目,在浏览器中访问Vue项目的地址,我的地址是 http://localhost:8080,点击按钮发送请求,获取的结果如下图所示。
在这里插入图片描述
到此,在Vue项目中就完成了简单的axios封装,你也可以根据自己的实际需求对axios进行封装,本文只是提供参考。

发表评论

表情:
评论列表 (有 0 条评论,291人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Vue统一封装Axios请求

    1.Axios 是什么,为什么要统一封装? axios是一个基于promise的http库,可运行在浏览器端和node.js中。他有很多优秀的特性,例如统一进行拦截请...