首页 > 其他分享 >axios封装loading加载

axios封装loading加载

时间:2022-08-25 17:23:29浏览次数:56  
标签:axios container transform loading 封装 div rotate animation

实现如下图效果

 

 点击获取数据按钮出现loading效果

1.定义一个Loading组件

<template>
  <div v-if="isShow" class="box">
    <div class="container">
      <div></div>
      <div></div>
      <div></div>
      <div></div>
      <div></div>
      <div></div>
      <div></div>
      <div></div>
      <div></div>
      <div></div>
      <div></div>
      <div></div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'Loading',
  data() {
    return {
      isShow: false
    }
  },
  methods: {
    show() {
      this.isShow = true
    },
    close() {
      this.isShow = false
    }
  }
}
</script>

<style >
.box {
  width: 100vw;
  height: 100vh;
  position: absolute;
  top: 0;
  left: 0;
  background-color: gray;
  opacity: 0.4;
}
.container {
  width: 150px;
  height: 150px;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

.container > div {
  position: absolute;
  left: 50%;
  top: 50%;
  margin-top: -10px;
  width: 50%;
  height: 20px;
  transform-origin: left center;
}

.container > div::after {
  content: '';
  position: absolute;
  right: 0;
  top: 0;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background-color: red;
  /* background-color: #000; */
  transform: var(--scale);
  animation: dotscale 1.2s linear infinite;
  animation-delay: var(--animation-delay);
}

.container > div:nth-child(1) {
  transform: rotate(0deg);
  --animation-delay: 0s;
}

.container > div:nth-child(2) {
  transform: rotate(30deg);
  --animation-delay: -0.1s;
}

.container > div:nth-child(3) {
  transform: rotate(60deg);
  --animation-delay: -0.2s;
}

.container > div:nth-child(4) {
  transform: rotate(90deg);
  --animation-delay: -0.3s;
}

.container > div:nth-child(5) {
  transform: rotate(120deg);
  --animation-delay: -0.4s;
}

.container > div:nth-child(6) {
  transform: rotate(150deg);
  --animation-delay: -0.5s;
}

.container > div:nth-child(7) {
  transform: rotate(180deg);
  --animation-delay: -0.6s;
}

.container > div:nth-child(8) {
  transform: rotate(210deg);
  --animation-delay: -0.7s;
}

.container > div:nth-child(9) {
  transform: rotate(240deg);
  --animation-delay: -0.8s;
}

.container > div:nth-child(10) {
  transform: rotate(270deg);
  --animation-delay: -0.9s;
}

.container > div:nth-child(11) {
  transform: rotate(300deg);
  --animation-delay: -1s;
}

.container > div:nth-child(12) {
  transform: rotate(330deg);
  --animation-delay: -1.1s;
}

@keyframes dotscale {
  0% {
    transform: scale(1);
    filter: hue-rotate(0deg);
  }

  100% {
    transform: scale(0);
    filter: hue-rotate(360deg);
  }
}
</style>

 

2.loading.js处理Loading组件,生成组件实例,添加对应的数据和方法

import Loadings from "./views/Loading.vue";

let $vm;
const plugin = {
  install(vue, option) {
    const Loading = vue.extend(Loadings);
    if (!$vm) {
      //创建组件实例
      $vm = new Loading({
        el: document.createElement("div"),
      });
      document.body.appendChild($vm.$el);
    }
    //定义两个方法,loading在根组件中
    const loading = {
      show() {
        //根组件调用Loading组件中定义的方法
        $vm.show();
      },
      hide() {
        $vm.close();
      },
    };
    //将loading挂载到根组件上
    if (!vue.$iView) {
      vue.$iView = {
        loading,
      };
    } else {
      vue.$iView.loading = loading;
    }
    vue.mixin({
      created: function () {
        this.$iView = vue.$iView;
      },
    });
  },
};
export default plugin;
export const install = plugin.install;

 

3.main.js引入封装axios

import loading from "./loading";
Vue.use(loading);
// 通过service调用loading,options为loading配置项
const service = axios.create({
  timeout: 10000,
});
//请求拦截
service.interceptors.request.use(
  (config) => {
    console.log(config);
    if (config.isLoading) {
      //loading
      Vue.$iView.loading.show();
    }
    //若超时,清除loading
    setTimeout(() => {
      Vue.$iView.loading.hide();
    }, 10000);
    return config;
  },
  (error) => {
    console.log(error);
    return Promise.reject();
  }
);
service.interceptors.response.use(
  (response) => {
    // 关闭loading
    Vue.$iView.loading.hide();
    if (response.status === 200) {
      return response.data;
    } else {
      Promise.reject();
    }
  },
  (error) => {
    console.log(error);
    return Promise.reject();
  }
);
Vue.prototype.$http = service;
new Vue({
  router,
  render: (h) => h(App),
}).$mount("#app");

 

标签:axios,container,transform,loading,封装,div,rotate,animation
From: https://www.cnblogs.com/lijun12138/p/16624958.html

相关文章

  • ASEMI整流桥DB307S参数,DB307S规格,DB307S封装
    编辑-ZASEMI整流桥DB307S参数:型号:DB307S最大重复峰值反向电压(VRRM):1000V最大RMS电桥输入电压(VRMS):700V最大直流阻断电压(VDC):1000V最大平均正向整流输出电流(IF):3A峰值正......
  • axios请求响应拦截器的应用
    什么是axios拦截器?一般在使用axios时,会用到拦截器的功能,一般分为两种:请求拦截器、响应拦截器。请求拦截器在请求发送前进行必要操作处理例如添加统一cookie、请求体加......
  • 封装文件导入组件,含导入进度条
    需求系统中需要有多个文件导入的地方,因此需要把它封装为组件便于复用。问题是:每次的导入url不同,每次封装的导入接口应该在主页面用呢?还是在组件页面用?解决办法分析:其......
  • 什么是双向链表?双向链表的操作封装实现(增删改查)?
    什么是双向链表?双向链表既可以从头遍历到尾,又可以从尾遍历到头也就是链表相连的过程是双向的.那么它的实现原理,你能猜到吗?一个节点既有向前连接的引用,也......
  • axios 取消请求
    axios参考文档:https://www.axios-http.cn/docs/cancellation 注:使用mock模块,模拟接口数据,个人测试,是无法取消请求的。  配置axios//axios.get(url,config)//如......
  • JS函数封装实现控件拖拽
    js脚本exportfunctiondragBox(drag,wrap){//用于获取父容器的样式属性值functiongetCss(ele,prop){//getComputedStyle返回值是带单位的字符串,所以......
  • 成员变量和局部变量的区别和面向对象的三大特征之封装性
    成员变量和局部变量的区别1、定义的位置不一样【重点】局部变量:在方法的内部成员变量:在方法的外部,直接鞋子类当中2、作用范围不一样【重点】局部变量:只有方法当中才可......
  • python 二次封装logging,打印日志文件名正确,且正确写入/结合pytest执行,日志不输出的问
    基于之前日志问题,二次封装日志后,导致日志输出的文件名不对,取到的文件一直都是当前二次封装的log的文件名,基于这个问题,做了优化,详细看https://www.cnblogs.com/cuitang/p/1......
  • JSONP及Axios
    JSONP及Axios一、jsonp1、概述概述:JSONP是一种跨域解决方案,它主要是利用了script标签不受跨域影响的特性来完成对应的请求操作。实际上是一个get请求。2、什么叫跨域(1......
  • 封装文件下载的方法
    1、在封装请求拦截的js文件中需要有以下准备条件:引入axios,在请求前加上token(这里token是从vuex中取的,这里不再阐述在vuex中的保存方法)1importaxiosfrom'axios'2......