当前位置:首页 > 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 中全局注册并使用。

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实现全局loading

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

相关文章

vue增删改查简单实现

vue增删改查简单实现

基础环境搭建 确保已安装Node.js和Vue CLI。通过以下命令创建Vue项目: npm install -g @vue/cli vue create vue-crud-demo cd vue…

vue为啥能实现监听

vue为啥能实现监听

Vue 实现监听的原理 Vue 的监听能力主要基于其响应式系统,该系统通过数据劫持和依赖收集实现。当数据变化时,系统能自动触发视图更新或执行回调函数。 核心机制:Object.definePrope…

vue实现公式

vue实现公式

在Vue中实现公式展示或计算可以通过多种方式实现,以下为具体方法: 使用模板插值与计算属性 若公式较简单,可直接在模板中使用插值表达式或计算属性。例如计算圆的面积: <templat…

jquery实现vue

jquery实现vue

jQuery 和 Vue.js 是两种不同理念的前端工具,jQuery 以 DOM 操作和事件驱动为核心,而 Vue.js 是数据驱动的现代化框架。如果想用 jQuery 实现类似 Vue 的功能(如…

通过vue实现

通过vue实现

Vue 实现方法 安装 Vue 确保已安装 Node.js 和 npm,通过以下命令安装 Vue CLI: npm install -g @vue/cli 创建 Vue 项目 使用 Vue CLI…

vue  实现tab

vue 实现tab

实现 Tab 切换功能 在 Vue 中实现 Tab 切换功能可以通过动态组件或条件渲染完成。以下是两种常见方法: 使用 v-if 或 v-show 实现条件渲染 通过绑定 currentTab 变量…