---
title: "VUE axios 加载中提示封装"
author: "AILIKES"
date: "2018-07-18T14:16:33Z"
tags: ["Ajax", "JavaScript", "UI"]
categories: ["Web前端"]
slug: "vue-axios"
draft: false
lastmod: "2018-07-18T14:16:33Z"
id: 2427081
url: "https://ailikes.iteye.com/blog/2427081"
---

```
import axios from 'axios'
// 引入axios以及element ui中的loading和message组件
import { MessageBox,Indicator } from 'mint-ui';
// 超时时间
axios.defaults.timeout = 5000
// http请求拦截器
var loadinginstace
axios.interceptors.request.use(config => {
    if(config.url.indexOf("?")!=-1){
      config.url = config.url + "&_v=" + genVersion();
    }else{
      config.url = config.url + "?_v=" + genVersion();
    }
  console.log(config.url);
  Indicator.open({
    //text: '加载中,请稍后...',
spinnerType: 'fading-circle'
});
  return config
}, error => {
  MessageBox.alert('数据加载超时,请检查您的网络或稍后重试!').then(action => {
    Indicator.close();
  });
  return Promise.reject(error)
})
// http响应拦截器
axios.interceptors.response.use(data => {// 响应成功关闭loading
Indicator.close();
  return data
}, error => {
  MessageBox.alert('数据加载失败!').then(action => {
    Indicator.close();
  });
  return Promise.reject(error)
})

export default axios;
```