当前位置:首页 > VUE

Vue全局loading实现

2026-01-17 05:42:10VUE

Vue全局loading实现

Vue全局loading实现

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

相关文章

Vue实现手机推送

Vue实现手机推送

Vue 中实现手机推送的方法 在 Vue 项目中实现手机推送功能,通常需要结合第三方推送服务(如 Firebase Cloud Messaging、极光推送等)或原生移动端能力(通过 Cordova/…

Vue实现路由监

Vue实现路由监

Vue 实现路由监听的方法 在 Vue 中可以通过多种方式实现路由监听,以下是常见的几种方法: 使用 watch 监听 $route 对象 通过 Vue 的 watch 功能可以监听 $route…

Vue实现鼠标悬浮

Vue实现鼠标悬浮

鼠标悬浮效果实现方法 在Vue中实现鼠标悬浮效果可以通过多种方式完成,以下是几种常见方法: 使用v-on指令绑定事件 通过@mouseenter和@mouseleave事件可以实现悬浮效果…

Vue拖拽怎么实现

Vue拖拽怎么实现

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

Vue实现过期退出

Vue实现过期退出

Vue实现过期退出功能的方法 在Vue应用中实现过期退出功能通常涉及以下步骤: 设置登录状态和过期时间 在用户登录成功后,将token和过期时间存储在本地存储或Vuex中: localStora…

Vue悬浮球怎么实现

Vue悬浮球怎么实现

Vue悬浮球实现方法 使用CSS定位和Vue事件绑定 创建一个Vue组件,利用CSS的position: fixed属性固定悬浮球位置,结合Vue的@click、@touchstart等事件实现交互…