当前位置:首页 > VUE

vue实现loading组件

2026-01-19 06:07:03VUE

Vue 实现 Loading 组件的方法

基础实现方案

创建一个独立的 Loading.vue 组件文件:

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

<script>
export default {
  props: {
    isLoading: {
      type: Boolean,
      default: false
    }
  }
}
</script>

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

.loading-spinner {
  border: 4px solid #f3f3f3;
  border-top: 4px solid #3498db;
  border-radius: 50%;
  width: 40px;
  height: 40px;
  animation: spin 1s linear infinite;
}

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

全局注册方案

main.js 中全局注册并使用:

vue实现loading组件

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

Vue.component('Loading', Loading)

// 或者作为插件使用
const LoadingPlugin = {
  install(Vue) {
    Vue.prototype.$loading = {
      show() {
        // 显示逻辑
      },
      hide() {
        // 隐藏逻辑
      }
    }
  }
}

Vue.use(LoadingPlugin)

使用 Vuex 管理状态

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

// 组件中使用
<template>
  <Loading :isLoading="$store.state.isLoading" />
</template>

高级实现方案

使用动态导入和异步组件:

vue实现loading组件

// 创建一个高阶组件
const asyncComponent = (componentImport) => {
  const AsyncComponent = () => ({
    component: componentImport,
    loading: LoadingComponent,
    delay: 200,
    timeout: 3000
  })
  return AsyncComponent
}

请求拦截方案

在 axios 拦截器中控制 loading 状态:

let loadingCount = 0

axios.interceptors.request.use(config => {
  loadingCount++
  store.commit('setLoading', true)
  return config
})

axios.interceptors.response.use(response => {
  loadingCount--
  if (loadingCount <= 0) {
    store.commit('setLoading', false)
  }
  return response
}, error => {
  loadingCount--
  if (loadingCount <= 0) {
    store.commit('setLoading', false)
  }
  return Promise.reject(error)
})

第三方库方案

使用现成的 loading 库:

npm install vue-loading-overlay
import Loading from 'vue-loading-overlay'
import 'vue-loading-overlay/dist/vue-loading.css'

Vue.use(Loading)

每种实现方案都有其适用场景,可以根据项目需求选择合适的实现方式。基础方案适合简单场景,全局方案适合大型应用,Vuex 方案适合状态管理复杂的项目,拦截器方案适合需要自动控制 loading 状态的场景。

标签: 组件vue
分享给朋友:

相关文章

vue实现上移下移插件

vue实现上移下移插件

实现上移下移功能的 Vue 插件 在 Vue 中实现列表项的上移和下移功能,可以通过自定义指令或组件封装的方式完成。以下是两种常见实现方式: 基于数组操作的通用方法 通过操作数组索引实现元素位置交换…

vue实现active

vue实现active

Vue 实现 active 状态的方法 在 Vue 中实现 active 状态可以通过以下几种方式实现,具体取决于使用场景和需求。 使用 v-bind:class 动态绑定类名 通过 v-bind:…

vue实现通讯

vue实现通讯

Vue 组件通讯方法 父子组件通讯 父组件向子组件传递数据通过 props,子组件向父组件传递数据通过 $emit 事件。 父组件模板: <child-component :message=…

vue实现标注

vue实现标注

Vue 实现标注功能的方法 使用 Vue 实现标注功能可以通过多种方式实现,以下介绍几种常见的方法: 1. 使用 HTML5 Canvas 实现标注 Canvas 提供了强大的绘图能力,适合实现复…

vue 实现循环

vue 实现循环

Vue 实现循环的方法 在 Vue 中,可以使用 v-for 指令来实现循环渲染列表数据。以下是几种常见的循环实现方式: 基本数组循环 <template> <ul>…

vue分页实现

vue分页实现

实现Vue分页的几种方法 使用Element UI的分页组件 安装Element UI库后,可以直接使用其分页组件。以下是一个基本示例: <template> <div>…