vue实现全局loading加蒙层

        在项目中会出现前端向后端发送请求,而请求的数据比较多的时候,页面会出现空白,假死现象,其实这是对使用功能是没有任何影响的,但是为了更好的用户体验,我们一般会在请求数据和获得后端给的数据这段时间加入以个友好的loading提示,如下图所示。本案例是基于vue2的。

 在src的components下创建loading包如下图

 index.vue的代码如下:

<template>
  <div v-if="show">
    <div class="loader-rainbow">
      <div class="loader-inner">
        <div class="loader-line-wrap">
          <div class="loader-line"></div>
        </div>
        <div class="loader-line-wrap">
          <div class="loader-line"></div>
        </div>
        <div class="loader-line-wrap">
          <div class="loader-line"></div>
        </div>
        <div class="loader-line-wrap">
          <div class="loader-line"></div>
        </div>
        <div class="loader-line-wrap">
          <div class="loader-line"></div>
        </div>
        <div class="box10" style="margin-top: 120px">拼命加载中....</div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "loading",
  props: {
    show: Boolean,
  },
  data() {
    return {};
  },
};
</script>

<style scoped>
.loader-rainbow {
  position: fixed;
  top: 50%;
  left: 50%;
  width: 100%;
  height: 100%;
  transform: translate(-50%, -50%);
  z-index: 9999;
  background-color: rgba(0,0,0,.5);
}
.loader-inner {
  bottom: 0;
  height: 100px;
  left: 0;
  margin: auto;
  position: absolute;
  right: 0;
  top: 0;
  width: 150px;
}

.loader-line-wrap {
  animation: spin 2000ms cubic-bezier(0.175, 0.885, 0.32, 1.275) infinite;
  box-sizing: border-box;
  height: 50px;
  left: 0;
  overflow: hidden;
  position: absolute;
  top: 0;
  transform-origin: 50% 100%;
  width: 100px;
}
.loader-line {
  border: 4px solid transparent;
  border-radius: 100%;
  box-sizing: border-box;
  height: 100px;
  left: 0;
  margin: 0 auto;
  position: absolute;
  right: 0;
  top: 0;
  width: 100px;
}
.loader-line-wrap:nth-child(1) {
  animation-delay: -50ms;
}
.loader-line-wrap:nth-child(2) {
  animation-delay: -100ms;
}
.loader-line-wrap:nth-child(3) {
  animation-delay: -150ms;
}
.loader-line-wrap:nth-child(4) {
  animation-delay: -200ms;
}
.loader-line-wrap:nth-child(5) {
  animation-delay: -250ms;
}

.loader-line-wrap:nth-child(1) .loader-line {
  border-color: hsl(0, 80%, 60%);
  height: 90px;
  width: 90px;
  top: 7px;
}
.loader-line-wrap:nth-child(2) .loader-line {
  border-color: hsl(60, 80%, 60%);
  height: 76px;
  width: 76px;
  top: 14px;
}
.loader-line-wrap:nth-child(3) .loader-line {
  border-color: hsl(120, 80%, 60%);
  height: 62px;
  width: 62px;
  top: 21px;
}
.loader-line-wrap:nth-child(4) .loader-line {
  border-color: hsl(180, 80%, 60%);
  height: 48px;
  width: 48px;
  top: 28px;
}
.loader-line-wrap:nth-child(5) .loader-line {
  border-color: hsl(240, 80%, 60%);
  height: 34px;
  width: 34px;
  top: 35px;
}

@keyframes spin {
  0%,
  15% {
    transform: rotate(0);
  }
  100% {
    transform: rotate(360deg);
  }
}

.box10 {
  background: linear-gradient(to bottom, rgb(107, 175, 240), rgb(245, 137, 227));
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  font-weight: bold;
  font-size: 20px;
}

</style>

index.js的代码如下:

        在渲染当前页面时,可能会发送多个请求,loadingNum记录着当前的请求个数,当当前请求的个数等于0个时候就会取消loading。

import Vue from 'vue'
import loading001 from './index.vue'

const LoadingConstructor = Vue.extend(loading001)

let loadingNum=0;
const instance = new LoadingConstructor({
  el: document.createElement('div')
})

instance.show = false
const loading = {
  show() {
    instance.show = true
    document.body.appendChild(instance.$el)
    loadingNum++
  },
  hide() {
    loadingNum--
    if(loadingNum===0){
      instance.show = false
    }
  }
}

export default {
  install() {
    if (!Vue.$showLoading) {
      Vue.$showLoading = loading
    }
    Vue.mixin({
      created() {
        this.$showLoading = Vue.$showLoading
      }
    })
  }
}

在前端的拦截器的代码:

// create an axios instance
const service = axios.create({
  // baseURL: process.env.VUE_APP_BASE_API, // url = base url + request url
  baseURL:baseURL,
  timeout: 10000 // request timeout
})

// request interceptor
service.interceptors.request.use(
  config => {
    Vue.$showLoading.show()
    return config
  },
  err => {
    return Promise.reject(err)
  }
)

// response interceptor
service.interceptors.response.use(
  response => {
    const res = response
    if (res.status !== 200) {
      Message({
        message: res.message || 'Error',
        type: 'error',
        duration: 5 * 1000
      })
    } else {
      Vue.$showLoading.hide()
      return res.data
    }
  },
  error => {
    console.log('err' + error) // for debug
    Message({
      message: error.message,
      type: 'error',
      duration: 5 * 1000
    })
    return Promise.reject(error)
  }
)

export default service

  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
可以通过 Vue 自定义指令来实现全局 loading 指令,具体实现步骤如下: 1. 在 Vue 实例中注册全局指令 ```javascript Vue.directive('loading', { // 指令选项 }) ``` 2. 在指令中定义 bind 和 unbind 钩子函数 ```javascript Vue.directive('loading', { bind: function (el, binding, vnode) { // 绑定指令时执行的操作 }, unbind: function (el, binding, vnode) { // 解绑指令时执行的操作 } }) ``` 3. 在 bind 钩子函数中创建 loading 元素,并将其添到页面中 ```javascript Vue.directive('loading', { bind: function (el, binding, vnode) { // 创建 loading 元素 const mask = document.createElement('div') mask.classList.add('loading-mask') const spinner = document.createElement('div') spinner.classList.add('loading-spinner') mask.appendChild(spinner) // 将 loading 元素添到页面中 el.appendChild(mask) }, unbind: function (el, binding, vnode) { // 解绑指令时执行的操作 } }) ``` 4. 在 bind 钩子函数中根据指令参数 show 控制 loading 元素的显示和隐藏 ```javascript Vue.directive('loading', { bind: function (el, binding, vnode) { // 创建 loading 元素 const mask = document.createElement('div') mask.classList.add('loading-mask') const spinner = document.createElement('div') spinner.classList.add('loading-spinner') mask.appendChild(spinner) // 将 loading 元素添到页面中 el.appendChild(mask) // 根据指令参数 show 控制 loading 元素的显示和隐藏 if (binding.value) { mask.style.display = 'block' } else { mask.style.display = 'none' } }, update: function (el, binding, vnode, oldVnode) { if (binding.oldValue !== binding.value) { if (binding.value) { el.querySelector('.loading-mask').style.display = 'block' } else { el.querySelector('.loading-mask').style.display = 'none' } } }, unbind: function (el, binding, vnode) { // 解绑指令时执行的操作 } }) ``` 5. 在 CSS 中定义 loading 元素的样式 ```css .loading-mask { position: absolute; top: 0; left: 0; bottom: 0; right: 0; background-color: rgba(255, 255, 255, 0.8); z-index: 9999; } .loading-spinner { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); width: 40px; height: 40px; border-radius: 50%; border: 5px solid #ccc; border-top-color: #333; animation: loading-spinner 1s linear infinite; } @keyframes loading-spinner { to { transform: rotate(360deg); } } ``` 最后,就可以在组件中使用 v-loading 指令来控制全局 loading 的显示和隐藏了。 ```html <template> <div v-loading="isLoading"> <!-- 页面内容 --> </div> </template> <script> export default { data() { return { isLoading: false } }, mounted() { // 模拟请求数据 this.isLoading = true setTimeout(() => { this.isLoading = false }, 2000) } } </script> ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值