当前位置:首页 > VUE

vue 实现loading

2026-01-13 04:22:28VUE

实现全局Loading组件

在Vue中可以通过自定义组件结合状态管理实现全局Loading效果。以下是一种常见实现方式:

创建Loading组件

vue 实现loading

<template>
  <div v-if="isLoading" class="loading-overlay">
    <div class="loading-spinner"></div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      isLoading: false
    }
  },
  methods: {
    show() {
      this.isLoading = true
    },
    hide() {
      this.isLoading = false
    }
  }
}
</script>

<style>
.loading-overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 9999;
}

.loading-spinner {
  border: 4px solid rgba(255, 255, 255, 0.3);
  border-radius: 50%;
  border-top: 4px solid #fff;
  width: 40px;
  height: 40px;
  animation: spin 1s linear infinite;
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}
</style>

注册为全局组件

在main.js中注册并挂载到Vue实例:

import Vue from 'vue'
import Loading from '@/components/Loading.vue'

const loading = Vue.extend(Loading)
const loadingInstance = new loading().$mount()
document.body.appendChild(loadingInstance.$el)

Vue.prototype.$loading = {
  show: () => loadingInstance.show(),
  hide: () => loadingInstance.hide()
}

在请求拦截器中使用

结合axios拦截器实现自动显示/隐藏:

vue 实现loading

import axios from 'axios'

axios.interceptors.request.use(config => {
  Vue.prototype.$loading.show()
  return config
}, error => {
  Vue.prototype.$loading.hide()
  return Promise.reject(error)
})

axios.interceptors.response.use(response => {
  Vue.prototype.$loading.hide()
  return response
}, error => {
  Vue.prototype.$loading.hide()
  return Promise.reject(error)
})

使用Vuex管理状态

对于大型项目,建议使用Vuex统一管理Loading状态:

// store.js
export default new Vuex.Store({
  state: {
    isLoading: false
  },
  mutations: {
    SET_LOADING(state, payload) {
      state.isLoading = payload
    }
  }
})

// 组件中使用
computed: {
  isLoading() {
    return this.$store.state.isLoading
  }
},
methods: {
  showLoading() {
    this.$store.commit('SET_LOADING', true)
  },
  hideLoading() {
    this.$store.commit('SET_LOADING', false)
  }
}

按需加载的局部Loading

对于局部区域可以使用v-loading指令:

Vue.directive('loading', {
  bind(el, binding) {
    const loadingDiv = document.createElement('div')
    loadingDiv.className = 'local-loading'
    el.loadingElement = loadingDiv

    if (binding.value) {
      el.appendChild(loadingDiv)
    }
  },
  update(el, binding) {
    if (binding.value) {
      el.appendChild(el.loadingElement)
    } else {
      el.removeChild(el.loadingElement)
    }
  }
})
.local-loading {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(255,255,255,0.7);
  z-index: 100;
}

标签: vueloading
分享给朋友:

相关文章

vue 实现长列表

vue 实现长列表

Vue 实现长列表的优化方案 虚拟滚动技术 虚拟滚动是处理长列表的高效方式,仅渲染可视区域内的元素。通过计算滚动位置动态加载和卸载DOM节点,大幅减少内存占用和渲染压力。 <template…

vue实现头像剪切

vue实现头像剪切

Vue 实现头像剪切的实现方法 使用 vue-cropperjs 库 vue-cropperjs 是一个基于 cropperjs 的 Vue 组件,专门用于图片裁剪。安装方式如下: npm inst…

vue怎么实现数据检测

vue怎么实现数据检测

Vue 数据检测的实现方式 Vue 通过响应式系统实现数据检测,核心机制基于 Object.defineProperty(Vue 2)或 Proxy(Vue 3)。以下是具体实现方法: 基于 Vu…

vue实现查询替换

vue实现查询替换

Vue 实现查询替换功能 在 Vue 中实现查询替换功能,可以通过数据绑定和字符串操作方法结合实现。以下是具体实现方式: 基础实现 <template> <div>…

vue实现弹窗可切换

vue实现弹窗可切换

实现弹窗可切换的 Vue 方案 动态组件切换 通过 Vue 的 <component :is="currentComponent"> 动态加载不同弹窗组件,结合 v-if 控制显示状态。…

vue中登陆实现逻辑

vue中登陆实现逻辑

Vue 中登录实现逻辑 前端实现逻辑 表单验证 使用 Vue 的表单验证库(如 VeeValidate)或自定义验证规则,确保用户输入的账号和密码符合要求。例如,检查邮箱格式、密码长度等。 发送登录…