当前位置:首页 > VUE

Vue全局loading实现

2026-03-29 22:29:10VUE

Vue 全局 Loading 的实现方法

通过 Vue 指令实现

创建一个自定义指令,用于在元素上显示或隐藏 loading 状态。在指令的钩子函数中,动态添加或移除 loading 元素。

// main.js
import Vue from 'vue'

Vue.directive('loading', {
  bind(el, binding) {
    const loadingDiv = document.createElement('div')
    loadingDiv.className = 'loading-mask'
    loadingDiv.innerHTML = '<div class="loading-spinner"></div>'
    el.loadingElement = loadingDiv

    if (binding.value) {
      el.appendChild(loadingDiv)
    }
  },
  update(el, binding) {
    if (binding.value) {
      el.appendChild(el.loadingElement)
    } else {
      el.removeChild(el.loadingElement)
    }
  }
})

// App.vue
<template>
  <div v-loading="isLoading"></div>
</template>

使用 Vue 插件封装

将 loading 功能封装为 Vue 插件,通过全局方法调用显示和隐藏 loading。

Vue全局loading实现

// loading.js
const Loading = {
  install(Vue) {
    Vue.prototype.$loading = {
      show() {
        // 显示 loading 逻辑
      },
      hide() {
        // 隐藏 loading 逻辑
      }
    }
  }
}

// main.js
import Loading from './loading'
Vue.use(Loading)

// 组件中使用
this.$loading.show()
this.$loading.hide()

结合 Vuex 状态管理

使用 Vuex 管理全局 loading 状态,在需要的地方通过 mutations 改变状态。

// store.js
const store = new Vuex.Store({
  state: {
    loading: false
  },
  mutations: {
    setLoading(state, payload) {
      state.loading = payload
    }
  }
})

// 组件中使用
this.$store.commit('setLoading', true)
this.$store.commit('setLoading', false)

第三方库实现

使用现成的第三方库如 element-ui 的 loading 组件或 vue-loading-overlay

Vue全局loading实现

// 使用 element-ui
import { Loading } from 'element-ui'

let loadingInstance = Loading.service(options)
loadingInstance.close()

// 使用 vue-loading-overlay
import Loading from 'vue-loading-overlay'
import 'vue-loading-overlay/dist/vue-loading.css'

Vue.use(Loading)

样式和动画

为 loading 添加样式和动画效果,提升用户体验。

.loading-mask {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(255, 255, 255, 0.7);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 9999;
}

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

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

请求拦截器集成

在 axios 拦截器中自动显示和隐藏 loading,减少手动调用的代码量。

// http.js
import axios from 'axios'

let loadingCount = 0

axios.interceptors.request.use(config => {
  loadingCount++
  showLoading()
  return config
})

axios.interceptors.response.use(response => {
  loadingCount--
  if (loadingCount === 0) {
    hideLoading()
  }
  return response
}, error => {
  loadingCount--
  if (loadingCount === 0) {
    hideLoading()
  }
  return Promise.reject(error)
})

标签: 全局Vue
分享给朋友:

相关文章

Vue论坛实现

Vue论坛实现

Vue论坛实现步骤 项目初始化 使用Vue CLI或Vite创建Vue 3项目。安装必要依赖如vue-router、axios、pinia/vuex。 npm create vue@latest v…

Vue 实现左右滑动

Vue 实现左右滑动

Vue 实现左右滑动的方法 使用 touch 事件监听 通过监听 touchstart、touchmove 和 touchend 事件实现基础滑动逻辑。在 Vue 组件中声明这些事件处理函数,计算滑动…

Vue全局loading实现

Vue全局loading实现

Vue全局Loading的实现方法 使用Vue插件封装全局Loading组件 创建一个独立的Loading组件,通过Vue插件机制注册为全局组件或方法。 <template> &…

Vue的扫雷实现

Vue的扫雷实现

Vue 扫雷实现步骤 准备工作 确保已安装 Vue CLI 或通过 CDN 引入 Vue。创建一个 Vue 项目或单文件组件,初始化游戏所需的数据结构(如雷区矩阵、雷数量、游戏状态等)。 数据结构设…

vue实现全局共享

vue实现全局共享

Vue 实现全局共享的方法 使用 Vuex 状态管理 Vuex 是 Vue 的官方状态管理库,适用于中大型应用。通过集中式存储管理所有组件的状态,实现全局共享。 安装 Vuex: npm inst…

Vue如何实现扫雷

Vue如何实现扫雷

Vue 实现扫雷游戏的核心思路 扫雷游戏的核心逻辑包括生成雷区、处理点击事件、计算周围雷数以及递归展开空白区域。Vue 的响应式特性非常适合管理游戏状态和更新视图。 数据结构设计 使用二维数组表示雷…