为vue项目自动设置请求状态的配置方法

作者:Kim09AI 时间:2024-04-30 10:44:26 

在进入一个页面的时候,一般在获取数据的同时,会先显示一个 loading ,等请求结束再隐藏 loading 渲染页面,只需要用一个属性去记录请求的状态,再根据这个状态去渲染页面就好了


async handler() {
 this.loading = true
 await fetch()
 this.loading = false
}

虽然是很简单的功能,可是要处理的地方多的时候,还是很繁琐的,就想着能不能统一设置处理请求的 loading ,然后页面根据 loading 的状态决定要显示的内容,就根据自己的想法做了一些封装,自动给所有 ajax 请求设置 loading 状态,主要思路是把所有请求集中到单一实例上,通过 proxy 代理属性访问,把 loading 状态提交到 store 的 state 中

安装

$ npm install vue-ajax-loading

演示

在线demo(打开较慢)

为vue项目自动设置请求状态的配置方法 

使用

配置 store 的 state 及 mutations


import { loadingState, loadingMutations } from 'vue-ajax-loading'

const store = new Vuex.Store({
 state: {
   ...loadingState
 },
 mutations: {
   ...loadingMutations
 }
})

把所有请求集中到一个对象上


import { ajaxLoading } from 'vue-ajax-loading'
import axios from 'axios'
import store from '../store' // Vuex.Store 创建的实例
axios.defaults.baseURL = 'https://cnodejs.org/api/v1'
// 把请求集中到单一对象上,如:
const service = {
 global: {
   // 全局的请求
   getTopics() {
     return axios.get('/topics')
   },
   getTopicById(id = '5433d5e4e737cbe96dcef312') {
     return axios.get(`/topic/${id}`)
   }
 },
 modules: {
   // 有命名空间的请求,命名空间就是 topic
   topic: {
     getTopics() {
       return axios.get('/topics')
     },
     getTopicById(id = '5433d5e4e737cbe96dcef312') {
       return axios.get(`/topic/${id}`)
     }
   }
 }
}

export default ajaxLoading({
 store,
 service
})

完成以上配置之后,通过上面 export default 出来的对象去发送请求,就会自动设置请求的状态,然后可以在组件内通过 this.$store.state.loading this.$loading 去访问请求状态,如:


<el-button type="primary" :loading="$loading.getTopics" @click="handler1">getTopics</el-button>
<el-button type="primary" :loading="$loading.delay" @click="delay">定时两秒</el-button>
<el-button type="primary" :loading="$loading.topic.getTopics" @click="handler3">topic.getTopics</el-button>

import api from 'path/to/api'
export default {
 methods: {
   handler1() {
     api.getTopics()
   },
   handler3() {
     api.topic.getTopics()
   },
   delay() {
     api.delay()
   }
 }
}

Options
store

Vuex.Store 创建的实例

service

包含所有请求的对象,可以配置 global 和 modules 属性

  • global:全局作用域的请求,可以设置为 对象 或 数组对象

  • modules:带命名空间的请求,类型为 对象 ,属性名即为命名空间

总结

以上所述是小编给大家介绍的为vue项目自动设置请求状态的配置方法,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!

来源:https://juejin.im/post/5cfb5fbd51882506400069ae

标签:vue,请求状态
0
投稿

猜你喜欢

  • Python logging自定义字段输出及打印颜色

    2023-09-03 17:04:25
  • asp如何实现对Session 数组的定义和调用?

    2010-05-18 18:40:00
  • sql server递归子节点、父节点sql查询表结构的实例

    2024-01-24 07:39:55
  • Django框架中方法的访问和查找

    2022-07-23 16:31:35
  • Python绘制1000响大地红鞭炮动态效果

    2021-08-21 20:44:26
  • 解决给dom元素绑定click等事件无效问题的方法

    2024-04-16 10:36:42
  • 如何使用postman(新手入门)

    2023-07-15 11:56:46
  • php下检测字符串是否是utf8编码的代码

    2023-11-15 16:00:59
  • MsSql 存储过程分页代码 [收集多篇]

    2024-01-13 13:13:33
  • Python 从subprocess运行的子进程中实时获取输出的例子

    2023-12-24 18:31:10
  • 使用Python写一个量化股票提醒系统

    2022-04-19 14:35:42
  • 推荐几款 Redis 可视化工具(太厉害了)

    2024-01-26 11:15:59
  • PHP mysqli扩展库 预处理技术的使用分析

    2023-11-21 07:10:21
  • String.indexOf 方法介绍

    2013-06-01 20:22:27
  • 在matlab中创建类似字典的数据结构方式

    2021-07-08 01:22:53
  • python爬虫爬取股票的k线图

    2021-06-30 15:54:37
  • Ethnique公司logo设计过程和思路

    2009-09-19 17:04:00
  • php之redis短线重连案例讲解

    2023-06-12 13:16:04
  • python xmind 包使用详解(其中解决导出的xmind文件 xmind8可以打开 xmind2020及之后版本打开报错问题)

    2022-10-01 17:23:57
  • TensorFLow用Saver保存和恢复变量

    2021-09-18 12:17:17
  • asp之家 网络编程 m.aspxhome.com