当前位置:首页 > 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
分享给朋友:

相关文章

vue组件的实现

vue组件的实现

Vue 组件的实现方式 Vue 组件可以通过多种方式实现,以下是常见的几种方法: 单文件组件 (SFC) 使用 .vue 文件,包含模板、脚本和样式三部分: <template>…

vue实现组件循环

vue实现组件循环

Vue 组件循环的实现方法 在 Vue 中实现组件循环通常使用 v-for 指令,可以循环渲染数组或对象的数据到组件上。 基本数组循环 通过 v-for 指令遍历数组,渲染多个相同结构的组件。…

vue实现多级弹窗

vue实现多级弹窗

Vue 多级弹窗实现方法 方法一:使用动态组件与递归组件 在 Vue 中可以通过动态组件配合递归组件实现多级弹窗。定义一个基础弹窗组件,通过递归调用自身实现层级嵌套。 基础弹窗组件模板示例: &…

vue前端实现登录

vue前端实现登录

实现登录功能的基本流程 使用Vue实现登录功能需要结合前端框架和后端API交互。以下是一个典型的实现方案: 安装必要依赖 确保项目已安装axios用于HTTP请求: npm install axi…

vue实现好评弹框

vue实现好评弹框

Vue 实现好评弹框的方法 使用 Vue 实现好评弹框可以通过组件化方式完成,结合动态数据绑定和事件处理。以下是具体实现步骤: 1. 创建弹框组件 新建一个 RatingDialog.vue 组件…

拖拽式编程vue实现

拖拽式编程vue实现

拖拽式编程在 Vue 中的实现方法 使用 HTML5 原生拖放 API Vue 可以结合 HTML5 的拖放 API 实现基础拖拽功能。通过 draggable 属性标记可拖拽元素,监听 dragst…