当前位置:首页 > 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 中实现选区创建通常涉及 DOM 操作和事件处理。以下是几种常见的方法: 使用原生 DOM API 通过 window.getSelection() 和 Ran…

vue实现下拉层级

vue实现下拉层级

Vue实现下拉层级菜单 使用Vue实现下拉层级菜单可以通过递归组件或动态渲染的方式完成。以下是两种常见实现方法: 递归组件实现 创建一个递归组件处理多级菜单结构: <template>…

vue实现modal

vue实现modal

Vue 实现 Modal 弹窗组件 使用 Vue 实现 Modal 弹窗组件可以通过多种方式,包括原生 Vue 组件、第三方库或自定义指令。以下是几种常见方法: 原生 Vue 组件实现 创建一个基础…

vue实现通讯

vue实现通讯

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

vue 实现平面

vue 实现平面

Vue 实现平面的方法 在 Vue 中实现平面效果通常涉及 CSS 样式、UI 框架或自定义组件的使用。以下是几种常见方法: 使用 CSS 样式 通过 Vue 的样式绑定或 scoped CSS 为…

vue jwt实现

vue jwt实现

Vue JWT 实现方法 安装依赖 确保项目中安装了 jsonwebtoken(后端)和 axios(前端)。若使用 Vue 3,可搭配 vue-router 和 pinia(或 vuex)管理状态。…