当前位置:首页 > 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 中注册插件。

vue实现全局loading

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

在组件中使用全局 loading。

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

使用 axios 拦截器实现请求 loading

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

vue实现全局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 中全局注册并使用。

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实现多级弹窗

vue实现多级弹窗

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

vue实现上移下移插件

vue实现上移下移插件

实现上移下移功能的 Vue 插件 在 Vue 中实现列表项的上移和下移功能,可以通过自定义指令或组件封装的方式完成。以下是两种常见实现方式: 基于数组操作的通用方法 通过操作数组索引实现元素位置交换…

vue架构实现

vue架构实现

Vue 架构实现核心方案 Vue.js 的架构实现通常围绕模块化、组件化和状态管理展开。以下为常见实现方案: 基于单文件组件(SFC)的模块化 通过 .vue 文件组织模板、脚本和样式: <…

vue 实现excel

vue 实现excel

Vue 实现 Excel 功能 前端导出 Excel 使用 xlsx 库可以轻松实现前端 Excel 导出功能。安装依赖: npm install xlsx 导出 Excel 示例代码: impo…

vue 实现赋值

vue 实现赋值

Vue 实现赋值的方法 在 Vue 中,赋值操作通常涉及数据绑定和响应式更新。以下是几种常见的赋值方式: 直接赋值给 data 属性 在 Vue 实例的 data 对象中定义的属性可以直接通过 th…

vue如何实现计算

vue如何实现计算

Vue 实现计算的方法 Vue 提供了多种方式来实现计算逻辑,包括计算属性、方法、侦听器等。以下是几种常见的实现方式: 计算属性(Computed Properties) 计算属性是基于它们的响应式…