当前位置:首页 > VUE

vue实现全局loading

2026-02-19 05:40:13VUE

Vue 实现全局 Loading 的方法

使用 Vuex 管理全局状态

在 Vuex 中定义 loading 状态和 mutations,用于控制 loading 的显示和隐藏。

// store.js
import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    isLoading: false
  },
  mutations: {
    showLoading(state) {
      state.isLoading = true
    },
    hideLoading(state) {
      state.isLoading = false
    }
  }
})

在组件中使用 mapState 获取 loading 状态,并在模板中根据状态显示 loading。

<template>
  <div>
    <div v-if="isLoading" class="loading-overlay">
      <div class="loading-spinner"></div>
    </div>
    <!-- 其他内容 -->
  </div>
</template>

<script>
import { mapState } from 'vuex'

export default {
  computed: {
    ...mapState(['isLoading'])
  }
}
</script>

使用插件封装全局 Loading

创建一个全局 loading 插件,通过 Vue.prototype 添加全局方法。

// loading.js
const Loading = {
  install(Vue) {
    Vue.prototype.$loading = {
      show() {
        // 显示 loading
      },
      hide() {
        // 隐藏 loading
      }
    }
  }
}

export default Loading

在 main.js 中注册插件。

import Loading from './loading'
Vue.use(Loading)

在组件中使用全局 loading。

this.$loading.show()
// 执行异步操作
this.$loading.hide()

使用 axios 拦截器实现请求 loading

在 axios 拦截器中统一处理 loading 的显示和隐藏。

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

const instance = axios.create()

instance.interceptors.request.use(config => {
  store.commit('showLoading')
  return config
})

instance.interceptors.response.use(response => {
  store.commit('hideLoading')
  return response
}, error => {
  store.commit('hideLoading')
  return Promise.reject(error)
})

export default instance

使用第三方库实现 loading

安装并引入第三方 loading 库,如 element-ui 的 loading 组件。

import { Loading } from 'element-ui'

let loadingInstance = Loading.service({
  fullscreen: true,
  text: '加载中...'
})

// 关闭 loading
loadingInstance.close()

自定义全局 loading 组件

创建一个全局 loading 组件,并通过动态挂载的方式控制显示和隐藏。

// GlobalLoading.vue
<template>
  <div v-if="isVisible" class="global-loading">
    <div class="loading-content">
      <!-- loading 内容 -->
    </div>
  </div>
</template>

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

在 main.js 中全局注册并使用。

vue实现全局loading

import GlobalLoading from './components/GlobalLoading'

const loadingComponent = Vue.extend(GlobalLoading)
const loadingInstance = new loadingComponent().$mount()
document.body.appendChild(loadingInstance.$el)

Vue.prototype.$loading = {
  show: () => loadingInstance.show(),
  hide: () => loadingInstance.hide()
}

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

相关文章

vue实现slider

vue实现slider

Vue 实现 Slider 组件 使用 Vue 实现 Slider 组件可以通过原生开发或借助第三方库。以下是两种常见方法: 原生实现 Slider 创建一个基础的 Slider 组件,通过 v-m…

vue实现通知

vue实现通知

Vue 实现通知功能的方法 使用 Vue 实现通知功能可以通过多种方式实现,以下是一些常见的方法: 使用 Vue 的全局事件总线 在 Vue 中可以通过事件总线实现通知功能。创建一个全局事件总线,然…

vue实现机制

vue实现机制

Vue 实现机制的核心原理 Vue.js 的核心实现机制主要基于响应式系统、虚拟 DOM 和组件化设计。以下是关键实现细节: 响应式系统 Vue 通过 Object.defineProperty(V…

vue 实现搜索

vue 实现搜索

实现 Vue 搜索功能 在 Vue 中实现搜索功能通常涉及以下几个关键步骤: 数据绑定与输入监听 使用 v-model 双向绑定搜索输入框的值,监听用户输入: <template>…

vue搜索实现

vue搜索实现

Vue 搜索功能实现方法 基于计算属性的搜索 在 Vue 中可以通过计算属性实现简单的搜索功能。这种方式适合小型数据集,利用 JavaScript 的 filter 方法进行筛选。 data() {…

vue实现treeview

vue实现treeview

Vue 实现 TreeView 的方法 使用递归组件实现 TreeView 递归组件是 Vue 中实现 TreeView 的常见方式。通过组件调用自身,可以轻松处理嵌套的树形结构数据。 <te…