当前位置:首页 > VUE

vue 实现loading

2026-01-13 04:22:28VUE

实现全局Loading组件

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

创建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拦截器实现自动显示/隐藏:

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;
}

vue 实现loading

标签: vueloading
分享给朋友:

相关文章

vue公共列表的实现

vue公共列表的实现

实现 Vue 公共列表组件 公共列表组件通常用于展示重复结构的数据,例如商品列表、用户列表等。以下是一个典型的实现方式: 基础实现 创建可复用的列表组件 List.vue: <templ…

vue手写签名如何实现

vue手写签名如何实现

实现手写签名的基本思路 在Vue中实现手写签名功能,通常需要借助HTML5的Canvas API来捕获用户的绘制操作。核心流程包括初始化画布、监听触摸/鼠标事件、记录轨迹以及生成签名图片。 安装依…

vue实现导航栏切图

vue实现导航栏切图

Vue 实现导航栏切图 使用 Vue Router 实现基础导航 在 Vue 项目中,可以通过 Vue Router 实现导航栏的路由切换。创建一个导航栏组件,并在其中使用 <router-li…

vue实现选中单元格

vue实现选中单元格

Vue 实现选中单元格的方法 基础实现思路 在 Vue 中实现选中单元格功能,通常可以通过动态绑定 class 或 style 来实现。以下是一个基于表格的简单实现示例: <templ…

vue实现签章

vue实现签章

Vue 实现签章功能 签章功能通常包括手写签名、电子印章等。以下是基于 Vue 的实现方法: 使用 canvas 实现手写签名 安装依赖(如需要): npm install signatu…

vue实现导出

vue实现导出

Vue 实现导出功能的方法 在 Vue 项目中实现导出功能通常涉及将数据导出为 Excel、CSV 或其他格式。以下是几种常见的实现方式: 使用 xlsx 库导出 Excel 安装 xlsx 库:…