当前位置:首页 > 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状态,便于跨组件控制。

vue实现全局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中引入:

vue实现全局loading

<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组件。

// 使用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
分享给朋友:

相关文章

eventbus vue实现

eventbus vue实现

EventBus 实现原理 EventBus 是一种发布/订阅模式,用于 Vue 组件间的通信。核心是通过一个中央事件总线管理事件监听和触发。 创建 EventBus 在 Vue 项目中创建一个独立…

vue 实现图钉

vue 实现图钉

实现图钉效果的步骤 在Vue中实现图钉(固定位置)效果,可以通过CSS的position: sticky属性或监听滚动事件动态修改元素样式。以下是两种常见方法: 方法一:使用CSS的posi…

vue实现文章滚动

vue实现文章滚动

Vue 实现文章滚动效果 使用 CSS 实现基础滚动 通过 CSS 的 overflow 和 height 属性控制滚动区域,适用于静态内容滚动。在 Vue 模板中定义固定高度的容器,内容超出后自动滚…

vue实现引导页面

vue实现引导页面

vue实现引导页面的方法 使用Vue实现引导页面可以通过多种方式完成,以下是几种常见的方法: 使用第三方库driver.js 安装driver.js库: npm install driver.j…

vue实现的页面

vue实现的页面

Vue 实现的页面示例 Vue.js 是一个渐进式 JavaScript 框架,用于构建用户界面。以下是一个简单的 Vue 页面实现示例,包含基本的结构和功能。 安装 Vue 通过 CDN 引入 V…

vue怎么实现滑动

vue怎么实现滑动

Vue实现滑动效果的方法 在Vue中实现滑动效果可以通过多种方式,以下是几种常见的方法: 使用CSS过渡和动画 通过Vue的过渡系统和CSS可以实现平滑的滑动效果。定义一个CSS类,利用transf…