当前位置:首页 > VUE

Vue全局loading实现

2026-01-17 05:42:10VUE

Vue全局Loading的实现方法

使用Vue插件封装全局Loading组件

创建一个独立的Loading组件,通过Vue插件机制注册为全局组件或方法。

<template>
  <div v-if="visible" class="global-loading">
    <div class="loading-spinner"></div>
    <p v-if="text">{{ text }}</p>
  </div>
</template>

<script>
export default {
  data() {
    return {
      visible: false,
      text: ''
    }
  },
  methods: {
    show(text = '') {
      this.text = text
      this.visible = true
    },
    hide() {
      this.visible = false
    }
  }
}
</script>

<style>
.global-loading {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  flex-direction: column;
  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 2s linear infinite;
}

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

创建Vue插件并注册

// loading-plugin.js
import LoadingComponent from './LoadingComponent.vue'

const LoadingPlugin = {
  install(Vue) {
    const LoadingConstructor = Vue.extend(LoadingComponent)
    const instance = new LoadingConstructor()
    instance.$mount(document.createElement('div'))
    document.body.appendChild(instance.$el)

    Vue.prototype.$loading = {
      show(text) {
        instance.show(text)
      },
      hide() {
        instance.hide()
      }
    }
  }
}

export default LoadingPlugin

在main.js中安装插件

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

Vue.use(LoadingPlugin)

在组件中使用全局Loading

// 显示loading
this.$loading.show('加载中...')

// 隐藏loading
this.$loading.hide()

结合axios拦截器实现请求自动loading

import axios from 'axios'

let loadingCount = 0

axios.interceptors.request.use(config => {
  if (loadingCount === 0) {
    Vue.prototype.$loading.show()
  }
  loadingCount++
  return config
})

axios.interceptors.response.use(
  response => {
    loadingCount--
    if (loadingCount === 0) {
      Vue.prototype.$loading.hide()
    }
    return response
  },
  error => {
    loadingCount--
    if (loadingCount === 0) {
      Vue.prototype.$loading.hide()
    }
    return Promise.reject(error)
  }
)

使用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
    }
  },
  actions: {
    showLoading({ commit }, text) {
      commit('SHOW_LOADING', text)
    },
    hideLoading({ commit }) {
      commit('HIDE_LOADING')
    }
  }
})

在组件中通过Vuex控制Loading

// 显示loading
this.$store.dispatch('showLoading', '加载中...')

// 隐藏loading
this.$store.dispatch('hideLoading')

以上方法提供了多种实现全局Loading的方案,可以根据项目需求选择适合的方式。插件方式适合简单项目,而结合Vuex的方式更适合大型复杂应用。

Vue全局loading实现

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

相关文章

Vue实现画布

Vue实现画布

Vue 实现画布的方法 在 Vue 中实现画布功能可以通过 HTML5 的 <canvas> 元素结合 Vue 的响应式特性来完成。以下是几种常见的实现方式: 使用原生 <canv…

Vue实现数据检验

Vue实现数据检验

Vue 数据校验的实现方法 在Vue中实现数据校验可以通过多种方式完成,以下是常见的几种方法: 使用Vue的内置指令 Vue提供了一些内置指令如v-model和v-bind可以结合HTML5的表单…

Vue 实现登出功能

Vue 实现登出功能

实现登出功能的方法 在Vue中实现登出功能通常涉及清除用户认证信息、重置应用状态并跳转到登录页面。以下是几种常见的实现方式: 清除本地存储的token localStorage.removeI…

Vue echarts实现散点图

Vue echarts实现散点图

Vue ECharts 实现散点图 安装 ECharts 在 Vue 项目中安装 ECharts 依赖: npm install echarts --save 引入 ECharts 在组件中引入…

Vue拖拽怎么实现

Vue拖拽怎么实现

Vue拖拽实现方法 使用Vue实现拖拽功能可以通过原生HTML5的拖拽API或第三方库如vuedraggable来实现。以下是两种常见的方法: 使用HTML5拖拽API HTML5提供了原生的拖拽A…

实现vue全局组件

实现vue全局组件

注册全局组件 在Vue中注册全局组件可以通过Vue.component方法完成。该方法接受两个参数:组件名称和组件选项对象。全局注册的组件可以在任何Vue实例的模板中使用,无需再次注册。 impor…