axios 拦截器显示和关闭Loading

更新日期: 2019-11-13阅读: 1.9k标签: axios

使用Loading分为2种情况,第一种是使用一些组件库自带的loading,另一种是使用我们自己写的loading,现分开介绍使用方法


一、使用element ui 带的Loading

1、在main.js 中引入axios 和elementui

// 引入element-ui 组件
import ElementUI from 'element-ui'
// 引入element-ui 样式文件
import 'element-ui/lib/theme-chalk/index.css'
import axios from "axios"
vue.use(ElementUI)
Vue.prototype.$axios = axios

2、在main.js 中设置Loading 显示和关闭函数

let loading;
function startLoading() {
	//如果根实例设为变量VUE var VUE = new Vue({}) 也可写成下面的 
    // loading = VUE.$loading({
    //   lock: true,
    //   text: "拼命加载中...",
    //   background: 'rgba(0,0,0,0.2)'
    // })
    loading = ElementUI.Loading.service({
        lock: true,
        text: "加载中...",
        background: 'rgba(0,0,0,0.2)'
    })
}

function endLoading() {
    loading.close()
}

3、同样在main.js 中设置请求和响应拦截器

// 请求数据拦截器
axios.interceptors.request.use(config => {
    console.log("拦截器")
    startLoading()
    return config
})
// 响应数据拦截器
axios.interceptors.response.use(response => {
    endLoading()
    return response
})


二、使用自己在页面中写的Loading

1、新建一个loading.vue 组件

<template>
  <div class="loader">
    <div class="loading" id="loading">
      <div class="animateWrap">
      <span></span>
      <span></span>
      <span></span>
      <span></span>
      <span></span>
      </div>
      <div class="text">正在加载</div>
    </div>
  </div>
</template>

<script>
// import theme from '@/assets/js/theme/shine.js'
export default {
  data() {
    return {};
  },

  methods: {},
  mounted(){
    var spanli = document.getElementById("loading").getElementsByTagName("span");
    for(var i=0;i<spanli.length;i++){
      spanli[i].style.webkitAnimationDelay = 0.13*i+"s"
    }
  }
};
</script>
<style>
.loader {
  position: fixed;
  left: 0;
  top: 0;
  bottom: 0;
  right: 0;
  background: rgba(0, 0, 0, 0.3);
  z-index: 20;
}
.loading {
  position: absolute;
  width: 70px;
  height: 70px;
  left: 0;
  right: 0;
  top: 0;
  bottom: 0;
  margin: auto;
 
}
.loading span {
  display: inline-block;
  width: 8px;
  height: 30px;
  border-radius: 3px;
  margin: 3px;
  background: #5af3f3;
  -webkit-animation:line-square 1s infinite;
}
.text{color:#5af3f3;}
@-webkit-keyframes line-square{
  0%{
    transform:scaleY(1)
  }
  50%{
    transform:scaleY(0.3)
  }
  100%{
    transform:scaleY(1)
  }
}
</style>

2、在App.vue 引入注册并使用loading.vue组件

<template>
    <div id="app">
        <ul class="nav" ref="nav">
            <router-link tag="li" :to='{name:"home"}'>首页</router-link>
            <router-link tag="li" :to="{name:'chart'}">图表</router-link>
            <router-link tag="li" :to="{name:'exportTable'}">表格</router-link>
            <router-link tag="li" :to="{name:'formTest'}">表单测试</router-link>
            <router-link tag="li" :to="{name:'layoutContainer'}">布局容器</router-link>
        </ul>
        {{message}}
        <router-view />
        <Loading v-if="this.$store.state.loadingShow" />
    </div>
</template>

<script>
import Loading from "@/components/loading.vue"
export default {
  name: 'App',
  data(){
    return{
     
    }    
  },
  components:{Loading},
}
</script>

3、在store 中初始化loadingShow

import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    loadingShow:false
  },
  mutations: {
  },
  actions: {
  },
  modules: {
  }
})

4、在main.js中设置请求和响应拦截器

// 请求数据拦截器
axios.interceptors.request.use(config => {
    console.log("拦截器")
    //startLoading()
    store.state.loadingShow = true
    return config
})
// 响应数据拦截器
axios.interceptors.response.use(response => {
    //endLoading()
    store.state.loadingShow = false
    return response
})

 

链接: https://www.fly63.com/article/detial/6823

axios处理Http请求的基本使用方法总汇

axios的功能特性:在浏览器中发送 XMLHttpRequests 请求,在 node.js 中发送 http请求,支持 Promise API,拦截请求和响应,转换请求和响应数据,自动转换 JSON 数据,客户端支持保护安全免受 XSRF 攻击

axios的特点与使用_解决处理axios兼容性问题

axios基于 Promise 的 HTTP 请求客户端,可同时在浏览器和 node.js 中使用。项目中发现,在安卓4.3及以下的手机不支持axios的使用,主要就是无法使用promise。加上以下polyfill就可以了。

axios常见传参方式_axios中get/post/put/patch请求

Axios 是一个基于 promise 的 HTTP 库,可以用在浏览器和 node.js 中。axios中get/post/put/patch请求。传参格式为 formData ,传参格式为 query 形式 ,传参格式为 raw等

axios-mock-adapter_一个axios调试好用的工具

axios-mock-adapter可以用来拦截http请求,并模拟响应,使用起来也很简单,比如你想模拟下服务器返回个500错误,什么404找不到、403禁止访问、500服务器错误、503服务不可用、504网关超时等等,你都能模拟出来

vue中axios的使用与封装

分享下我自己的axios封装,axios是个很好用的插件,都是一些params对象,所以很方便做一些统一处理。当然首先是npm安装axios 很简单。在src下新建文件夹 service / index.js,接着上代码

vue axios不缓存get请求(防止返回304不更新数据)

最近做项目遇到一款浏览器,由于缓存了get请求,导致不管如何刷新,数据都不更新的问题。以下分享一下解决办法:解决思路就是给每一条get请求增加一个timestamp的参数,value为时间戳

vue中axios请求的封装

发送请求模块目录,@/api/url中存放的是每个模块的URL,使用webpack提供的require.context将src/api/url下后缀为js的所有文件引入,并整理出一个对象。整合common.js & product.js,最终得到的对象如下:

axios基于常见业务场景的二次封装

axios的二次封装,功能实现:1.兼容ie浏览器避免缓存2.减少或更新重复请求3.接口域名使用环境变量4.全局loading状态5.可关闭的全局错误提醒6.可开启携带全局分页参数

Vue+Typescript中在Vue上挂载axios使用时报错

在vue项目开发过程中,为了方便在各个组件中调用axios,我们通常会在入口文件将axios挂载到vue原型身上,如下:这样的话,我们在各个组件中进行请求时

vue axios 拦截器

项目中需要验证登录用户身份是否过期,是否有权限进行操作,所以需要根据后台返回不同的状态码进行判断。axios的拦截器分为请求拦截器和响应拦截器两种。我一般把拦截器写在main.js里。

点击更多...

内容以共享、参考、研究为目的,不存在任何商业目的。其版权属原作者所有,如有侵权或违规,请与小编联系!情况属实本人将予以删除!