vue中api统一管理

简介: 【10月更文挑战第4天】

针对小型项目
无需管理的情况下

<script>
import axios from 'axios';
export default {
   
  methods: {
   
    async request() {
   
      let data = {
   }
      try {
   
        // host指的是请求的域名,path指的是请求的路径, data是相关的参数和请求头配置
        let res = await axios.post(`${
     host}${
     path}`, {
   
          data
        })
        console.log(res)
      } catch(err) {
   
        this.$message.error(err.message)
      }
    }
  }
}
</script>

统一api.js文件管理
将所有的api的接口信息都写在一个js文件里去维护。页面接口请求直接引入即可

在根目录下创建api文件夹,然后创建index.js

export default {
   
  getInfo: 'https://xxx.x.com/getinfo'
}

具体页面使用

<script>
import axios from 'axios';
import api from '@/api/index';
export default {
   
  methods: {
   
    async request() {
   
      let data = {
   }
      try {
   
        let res = await axios.post(api.getInfo, {
   
          data
        })
        console.log(res)
      } catch(err) {
   
        this.$message.error(err.message)
      }

    }
  }
}
</script>

针对非小型项目
api统一管理 + 挂载到vue实例上 + 单模块
思路:在api统一管理时,不仅仅管理请求地址,而是直接写一个request的请求方法,通过接受一些参数来实现多变性。
api/index.js

import request from '@/utils/axios'
export default {
   
  getInfo(params) {
   
    return request({
   
      url: '/xxx/xxx/xxx',
      method: 'post/get',
      params, // 如果是get请求的话
      data: params // 如果是post请求的话
    })
  }
}

在main.js里

import Vue from 'vue'
import App from './App.vue'
import api from '@/api/index';
Vue.prototype.$api = api;
Vue.config.productionTip = false
new Vue({
   
  render: h => h(App),
}).$mount('#app')

页面上得使用

<script>
import HelloWorld from './components/HelloWorld.vue'
import api from '@/api/index';
export default {
   
  methods: {
   
    async request() {
   
      let data = {
   }
      try {
   
        let res = await this.$api.getInfo(data)
        console.log(res)
      } catch(err) {
   
        this.$message.error(err.message)
      }
    }
  }
}
</script>

api统一管理 + 挂载到vue实例上 + 多模块
优点:可以在任意位置调用接口
缺点:如果接口数量足够大,挂载到vue实例上得数据过多,可能会造成性能问题
api/modules/account.js

import account from '@/utils/axios'
export default {
   
  getInfo(params) {
   
    return request({
   
      url: '/xxx/xxx/xxx',
      method: 'post/get',
      params, // 如果是get请求的话
      data: params // 如果是post请求的话
    })
  }
}

api/index.js

import account from './modules/account'
export default {
   
  account
}

在main.js里

import Vue from 'vue'
import App from './App.vue'
import api from '@/api/index';
Vue.prototype.$api = api;
Vue.config.productionTip = false
new Vue({
   
  render: h => h(App),
}).$mount('#app')

页面上的使用

<script>
import HelloWorld from './components/HelloWorld.vue'
import api from '@/api/index';
export default {
   
  methods: {
   
    async request() {
   
      let data = {
   }
      try {
   
        let res = await this.$api.account.getInfo(data)
        console.log(res)
      } catch(err) {
   
        this.$message.error(err.message)
      }
    }
  }
}
</script>

api统一管理 + vuex + 单模块
思路:api统一管理的方式不变,但是由挂载到vue实例上改成vuex 优点:在不挂载到vue实例的基础上可以在任何页面随意调用任何接口 缺点:为了保证在刷新页面不会报错的情况下就需要在api模块写一个接口配置,同时在store模块也需要写一次,比较繁琐。
在api/index.js的写法不变。
main.js中的相关挂载代码删除
store/index.js

import Vue from 'vue';
import Vuex from 'vuex';
import api from '@/api/index';
Vue.use(Vuex);
export default new Vuex.Store({
   
  action: {
   
    getInfo(store, params) {
   
      return api.getInfo(params)
    }
  }
})

在页面中

<script>
export default {
   
  methods: {
   
    async request() {
   
      let data = {
   }
      try {
   
        let res = await this.$store.dispatch('getInfo', data)
        console.log(res)
      } catch(err) {
   
        this.$message.error(err.message)
      }
    }
  }
}
</script>

当然你也可以使用mapActions

<script>
import {
    mapActions } from 'vuex';
export default {
   
  methods: {
   
    ...mapActions([ 'getInfo' ])
    async request() {
   
      let data = {
   }
      try {
   
        let res = await this.getInfo(data)
        console.log(res)
      } catch(err) {
   
        this.$message.error(err.message)
      }
    }
  }
}
</script>

api统一管理 + vuex + 多模块
优点:可以在页面的任何位置进行调用 缺点:新增删除修改同一个接口,需要同时维护两个文件
对于api文件而言,此时各个模式是相互独立的:api/account.js

import request from '@/utils/axios'
export default {
   
  getInfo(params) {
   
    return request({
   
      url: '/xxx/xxx/xxx',
      method: 'post/get',
      params, // 如果是get请求的话
      data: params // 如果是post请求的话
    })
  }
}

store/modules/account.js

import api from '@/api/account';
export default {
   
    namespaced: true,
    actions: {
   
        getInfo(store, params) {
   
          return api.getInfo(params)
        }
    }
}

store/index.js

import Vue from 'vue';
import Vuex from 'vuex';
import account from './modules/account';
Vue.use(Vuex);
export default new Vuex.Store({
   
  modules: {
   
      account
  }
})

在页面中

<script>
export default {
   
  methods: {
   
    async request() {
   
      let data = {
   }
      try {
   
        let res = await this.$store.dispatch('account/getInfo', data)
        console.log(res)
      } catch(err) {
   
        this.$message.error(err.message)
      }
    }
  }
}
</script>
相关文章
|
27天前
|
弹性计算 人工智能 架构师
阿里云携手Altair共拓云上工业仿真新机遇
2024年9月12日,「2024 Altair 技术大会杭州站」成功召开,阿里云弹性计算产品运营与生态负责人何川,与Altair中国技术总监赵阳在会上联合发布了最新的“云上CAE一体机”。
阿里云携手Altair共拓云上工业仿真新机遇
|
3天前
|
人工智能 Rust Java
10月更文挑战赛火热启动,坚持热爱坚持创作!
开发者社区10月更文挑战,寻找热爱技术内容创作的你,欢迎来创作!
370 16
|
19天前
|
存储 关系型数据库 分布式数据库
GraphRAG:基于PolarDB+通义千问+LangChain的知识图谱+大模型最佳实践
本文介绍了如何使用PolarDB、通义千问和LangChain搭建GraphRAG系统,结合知识图谱和向量检索提升问答质量。通过实例展示了单独使用向量检索和图检索的局限性,并通过图+向量联合搜索增强了问答准确性。PolarDB支持AGE图引擎和pgvector插件,实现图数据和向量数据的统一存储与检索,提升了RAG系统的性能和效果。
|
6天前
|
JSON 自然语言处理 数据管理
阿里云百炼产品月刊【2024年9月】
阿里云百炼产品月刊【2024年9月】,涵盖本月产品和功能发布、活动,应用实践等内容,帮助您快速了解阿里云百炼产品的最新动态。
阿里云百炼产品月刊【2024年9月】
|
21天前
|
人工智能 IDE 程序员
期盼已久!通义灵码 AI 程序员开启邀测,全流程开发仅用几分钟
在云栖大会上,阿里云云原生应用平台负责人丁宇宣布,「通义灵码」完成全面升级,并正式发布 AI 程序员。
|
23天前
|
机器学习/深度学习 算法 大数据
【BetterBench博士】2024 “华为杯”第二十一届中国研究生数学建模竞赛 选题分析
2024“华为杯”数学建模竞赛,对ABCDEF每个题进行详细的分析,涵盖风电场功率优化、WLAN网络吞吐量、磁性元件损耗建模、地理环境问题、高速公路应急车道启用和X射线脉冲星建模等多领域问题,解析了问题类型、专业和技能的需要。
2592 22
【BetterBench博士】2024 “华为杯”第二十一届中国研究生数学建模竞赛 选题分析
|
5天前
|
存储 人工智能 搜索推荐
数据治理,是时候打破刻板印象了
瓴羊智能数据建设与治理产品Datapin全面升级,可演进扩展的数据架构体系为企业数据治理预留发展空间,推出敏捷版用以解决企业数据量不大但需构建数据的场景问题,基于大模型打造的DataAgent更是为企业用好数据资产提供了便利。
181 2
|
3天前
|
编译器 C#
C#多态概述:通过继承实现的不同对象调用相同的方法,表现出不同的行为
C#多态概述:通过继承实现的不同对象调用相同的方法,表现出不同的行为
105 65
|
7天前
|
Linux 虚拟化 开发者
一键将CentOs的yum源更换为国内阿里yum源
一键将CentOs的yum源更换为国内阿里yum源
332 2
|
23天前
|
机器学习/深度学习 算法 数据可视化
【BetterBench博士】2024年中国研究生数学建模竞赛 C题:数据驱动下磁性元件的磁芯损耗建模 问题分析、数学模型、python 代码
2024年中国研究生数学建模竞赛C题聚焦磁性元件磁芯损耗建模。题目背景介绍了电能变换技术的发展与应用,强调磁性元件在功率变换器中的重要性。磁芯损耗受多种因素影响,现有模型难以精确预测。题目要求通过数据分析建立高精度磁芯损耗模型。具体任务包括励磁波形分类、修正斯坦麦茨方程、分析影响因素、构建预测模型及优化设计条件。涉及数据预处理、特征提取、机器学习及优化算法等技术。适合电气、材料、计算机等多个专业学生参与。
1580 17
【BetterBench博士】2024年中国研究生数学建模竞赛 C题:数据驱动下磁性元件的磁芯损耗建模 问题分析、数学模型、python 代码