当前位置:首页 > VUE

vue实现全局加载动画

2026-01-21 13:32:24VUE

使用Vue插件封装全局加载动画

在Vue项目中创建一个自定义插件,通过Vue的插件系统实现全局调用。

创建src/plugins/loading.js文件:

const Loading = {
  install(Vue) {
    Vue.prototype.$loading = {
      show() {
        const loadingEl = document.createElement('div')
        loadingEl.id = 'global-loading'
        loadingEl.innerHTML = `
          <div class="loading-overlay">
            <div class="loading-spinner"></div>
          </div>
        `
        document.body.appendChild(loadingEl)
      },

      hide() {
        const loadingEl = document.getElementById('global-loading')
        if (loadingEl) {
          document.body.removeChild(loadingEl)
        }
      }
    }
  }
}

export default Loading

添加加载动画样式

在项目的全局CSS文件(如src/assets/styles/global.css)中添加样式:

.loading-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.5);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 9999;
}

.loading-spinner {
  width: 50px;
  height: 50px;
  border: 5px solid #f3f3f3;
  border-top: 5px solid #3498db;
  border-radius: 50%;
  animation: spin 1s linear infinite;
}

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

注册插件并全局使用

main.js中注册插件:

import Vue from 'vue'
import Loading from './plugins/loading'

Vue.use(Loading)

在组件中使用:

// 显示加载动画
this.$loading.show()

// 隐藏加载动画
this.$loading.hide()

使用axios拦截器实现自动加载动画

可以与axios拦截器结合,实现请求时自动显示加载动画:

import axios from 'axios'

// 请求拦截器
axios.interceptors.request.use(config => {
  Vue.prototype.$loading.show()
  return config
}, error => {
  Vue.prototype.$loading.hide()
  return Promise.reject(error)
})

// 响应拦截器
axios.interceptors.response.use(response => {
  Vue.prototype.$loading.hide()
  return response
}, error => {
  Vue.prototype.$loading.hide()
  return Promise.reject(error)
})

使用Vuex管理加载状态

对于更复杂的应用,可以使用Vuex集中管理加载状态:

创建store模块:

const loadingModule = {
  state: () => ({
    isLoading: false
  }),
  mutations: {
    SET_LOADING(state, status) {
      state.isLoading = status
    }
  },
  actions: {
    showLoading({ commit }) {
      commit('SET_LOADING', true)
    },
    hideLoading({ commit }) {
      commit('SET_LOADING', false)
    }
  }
}

在根组件中使用v-if控制加载动画显示:

vue实现全局加载动画

<template>
  <div>
    <div v-if="$store.state.loading.isLoading" class="global-loading">
      <!-- 加载动画内容 -->
    </div>
    <router-view />
  </div>
</template>

标签: 全局加载
分享给朋友:

相关文章

vue实现加载

vue实现加载

Vue 实现加载功能的方法 使用 v-if 或 v-show 控制加载状态 通过数据绑定控制加载动画的显示与隐藏。v-if 会完全移除 DOM 元素,v-show 只是切换 display 属性。…

vue加载动画实现

vue加载动画实现

Vue 加载动画的实现方法 在 Vue 中实现加载动画可以通过多种方式完成,以下是一些常见的方法: 使用 CSS 动画 通过 CSS 定义动画效果,结合 Vue 的动态类绑定或条件渲染控制动画的显示…

vue  全局刷新实现

vue 全局刷新实现

Vue 全局刷新实现方法 在Vue应用中实现全局刷新通常涉及重新加载整个页面或重置应用状态。以下是几种常见方法: 使用 window.location.reload() 直接调用浏览器原生的刷新方法…

vue实现图片加载

vue实现图片加载

Vue 实现图片加载的方法 在 Vue 中实现图片加载可以通过多种方式,以下是一些常见的方法: 使用 v-bind 绑定图片路径 通过 v-bind 动态绑定图片路径,可以灵活地加载本地或远程图片。…

vue加载实现分页

vue加载实现分页

Vue 实现分页加载的方法 使用 Element UI 的分页组件 安装 Element UI 后,可以直接使用其分页组件。在模板中添加分页组件,并绑定相关事件和数据。 <template&g…

vue实现触底加载

vue实现触底加载

触底加载的实现方法 在Vue中实现触底加载功能,可以通过监听滚动事件或使用Intersection Observer API来实现。以下是两种常见的方法: 监听滚动事件 在组件中监听滚动事件,计算…