当前位置:首页 > VUE

vue实现全局loading

2026-01-18 13:13:27VUE

使用Vue插件实现全局Loading

在Vue项目中创建一个自定义插件,通过插件机制实现全局Loading功能。

// loading-plugin.js
const LoadingPlugin = {
  install(Vue) {
    const LoadingComponent = Vue.extend({
      template: `
        <div v-if="show" class="global-loading">
          <div class="loading-spinner"></div>
          <div class="loading-text">{{ text }}</div>
        </div>
      `,
      data() {
        return {
          show: false,
          text: '加载中...'
        }
      }
    })

    const loadingInstance = new LoadingComponent({
      el: document.createElement('div')
    })

    document.body.appendChild(loadingInstance.$el)

    Vue.prototype.$loading = {
      show(text) {
        loadingInstance.text = text || '加载中...'
        loadingInstance.show = true
      },
      hide() {
        loadingInstance.show = false
      }
    }
  }
}

export default LoadingPlugin

在main.js中注册插件:

import Vue from 'vue'
import LoadingPlugin from './loading-plugin'

Vue.use(LoadingPlugin)

使用Vuex管理Loading状态

通过Vuex集中管理全局Loading状态,便于跨组件控制。

// store.js
export default new Vuex.Store({
  state: {
    loading: false,
    loadingText: '加载中...'
  },
  mutations: {
    SHOW_LOADING(state, text) {
      state.loading = true
      state.loadingText = text || '加载中...'
    },
    HIDE_LOADING(state) {
      state.loading = false
    }
  }
})

创建全局Loading组件:

// GlobalLoading.vue
<template>
  <div v-if="$store.state.loading" class="global-loading">
    <div class="loading-spinner"></div>
    <div class="loading-text">{{ $store.state.loadingText }}</div>
  </div>
</template>

<script>
export default {
  name: 'GlobalLoading'
}
</script>

在App.vue中引入:

<template>
  <div id="app">
    <GlobalLoading />
    <router-view />
  </div>
</template>

使用axios拦截器实现请求Loading

结合axios拦截器自动显示和隐藏Loading状态。

// http.js
import axios from 'axios'
import store from './store'

const http = axios.create()

let requestCount = 0

http.interceptors.request.use(config => {
  requestCount++
  store.commit('SHOW_LOADING', config.loadingText)
  return config
})

http.interceptors.response.use(
  response => {
    requestCount--
    if (requestCount === 0) {
      store.commit('HIDE_LOADING')
    }
    return response
  },
  error => {
    requestCount--
    if (requestCount === 0) {
      store.commit('HIDE_LOADING')
    }
    return Promise.reject(error)
  }
)

export default http

使用CSS样式优化Loading效果

为Loading组件添加样式增强视觉效果。

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

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

.loading-text {
  margin-top: 20px;
  color: white;
  font-size: 16px;
}

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

使用第三方库实现高级Loading效果

对于更复杂的需求,可以考虑使用第三方库如element-uivant的Loading组件。

vue实现全局loading

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

let loadingInstance

Vue.prototype.$loading = {
  show() {
    loadingInstance = Loading.service({
      lock: true,
      text: '加载中',
      spinner: 'el-icon-loading',
      background: 'rgba(0, 0, 0, 0.7)'
    })
  },
  hide() {
    loadingInstance.close()
  }
}

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

相关文章

vue实现图片搜索

vue实现图片搜索

Vue实现图片搜索功能 使用HTML5的File API获取图片 通过<input type="file">元素让用户选择图片文件,使用FileReader对象读取图片数据。 <t…

vue前端实现下载进度

vue前端实现下载进度

实现下载进度条的基本思路 在Vue中实现下载进度条,通常需要结合XMLHttpRequest或Fetch API来监听下载进度事件。通过计算已下载数据与总数据的比例,动态更新进度条的显示。 使用XM…

vue实现波形

vue实现波形

实现波形效果的方法 在Vue中实现波形效果可以通过多种方式完成,常见的方法包括使用Canvas绘制、CSS动画或第三方库。以下是几种实现方案: 使用Canvas绘制波形 通过Canvas API动态…

vue实现select

vue实现select

Vue 实现 Select 组件的方法 在 Vue 中实现 Select 组件可以通过多种方式完成,以下是几种常见的方法: 使用原生 HTML select 元素 原生 HTML 的 <sel…

vue 实现ztree

vue 实现ztree

在Vue中实现类似ZTree的功能,可以通过集成第三方库或自定义组件完成。以下是两种常见方法: 使用vue-tree组件 vue-tree是一个轻量级的Vue树形组件,支持复选框、懒加载等功能,适合…

vue 实现协同

vue 实现协同

Vue 实现协同编辑的方案 协同编辑指多个用户同时编辑同一文档并实时同步更改。Vue 结合相关库和技术可轻松实现该功能。 使用 WebSocket 实现实时通信 WebSocket 提供全双工通信,…