当前位置:首页 > VUE

vue 怎么实现loading

2026-03-30 10:54:09VUE

实现全局 Loading 的方法

在 Vue 中可以通过封装一个全局组件配合 Vuex 或 Pinia 状态管理实现。创建一个 Loading.vue 组件,通过 v-if 控制显示隐藏:

<template>
  <div v-if="isLoading" class="loading-overlay">
    <div class="spinner"></div>
  </div>
</template>

<script>
export default {
  computed: {
    isLoading() {
      return this.$store.state.isLoading
    }
  }
}
</script>

在 Vuex 中定义状态并提交 mutation 控制:

const store = new Vuex.Store({
  state: {
    isLoading: false
  },
  mutations: {
    setLoading(state, status) {
      state.isLoading = status
    }
  }
})

指令式 Loading 的实现

通过 Vue 指令可以在任意元素上触发 Loading 状态。注册一个 v-loading 指令:

Vue.directive('loading', {
  bind(el, binding) {
    const loadingDiv = document.createElement('div')
    loadingDiv.className = 'loading-mask'
    el.loadingElement = loadingDiv

    if (binding.value) {
      el.appendChild(loadingDiv)
    }
  },
  update(el, binding) {
    if (binding.value !== binding.oldValue) {
      if (binding.value) {
        el.appendChild(el.loadingElement)
      } else {
        el.removeChild(el.loadingElement)
      }
    }
  }
})

请求拦截器集成

在 axios 拦截器中自动管理 Loading 状态:

axios.interceptors.request.use(config => {
  store.commit('setLoading', true)
  return config
})

axios.interceptors.response.use(
  response => {
    store.commit('setLoading', false)
    return response
  },
  error => {
    store.commit('setLoading', false)
    return Promise.reject(error)
  }
)

组件级 Loading 控制

在单个组件内部管理 Loading 状态:

<template>
  <button @click="fetchData" :disabled="isLoading">
    {{ isLoading ? 'Loading...' : 'Submit' }}
  </button>
</template>

<script>
export default {
  data() {
    return {
      isLoading: false
    }
  },
  methods: {
    async fetchData() {
      this.isLoading = true
      try {
        await axios.get('/api/data')
      } finally {
        this.isLoading = false
      }
    }
  }
}
</script>

第三方库的使用

常用 Loading 库如 element-ui 提供现成方案:

import { Loading } from 'element-ui'

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

// 关闭加载
loadingInstance.close()

对于 Vue 3 项目,可以使用 vue-loading-overlay

import Loading from 'vue-loading-overlay'
import 'vue-loading-overlay/dist/vue-loading.css'

app.component('Loading', Loading)

动画效果优化

为 Loading 添加 CSS 动画提升用户体验:

.loading-spinner {
  animation: spin 1s linear infinite;
}

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

按需加载配置

通过动态导入实现 Loading 组件的按需加载:

vue 怎么实现loading

const LoadingComponent = defineAsyncComponent({
  loader: () => import('./Loading.vue'),
  loadingComponent: SimpleLoading,
  delay: 200
})

标签: vueloading
分享给朋友:

相关文章

vue登录业务的实现

vue登录业务的实现

实现登录业务的基本流程 在Vue中实现登录业务通常涉及前端界面设计、表单验证、API请求交互、状态管理以及路由控制等环节。以下是核心实现步骤: 登录表单设计与验证 创建包含用户名和密码输入框的登录组…

vue实现图集

vue实现图集

Vue 实现图集的方法 在 Vue 中实现图集功能可以通过多种方式完成,以下是一些常见的方法和实现步骤。 使用第三方库(如 vue-image-lightbox) 安装 vue-image-ligh…

vue实现computed

vue实现computed

Vue 中 computed 的实现原理 computed 属性是 Vue 的一个核心特性,它基于响应式依赖进行缓存,仅在依赖发生变化时重新计算。以下是 computed 的实现方式: 基本用法 在…

vue实现openoffice

vue实现openoffice

Vue 中集成 OpenOffice 的实现方法 在 Vue 项目中集成 OpenOffice 通常需要通过后端服务或现有库实现文档的预览和编辑功能。以下是几种常见的实现方式: 使用 OnlyOff…

vue实现侧导航

vue实现侧导航

Vue 实现侧边导航 使用 Vue 实现侧边导航可以通过多种方式完成,以下是一种常见的实现方法,结合 Vue Router 和动态组件。 基础结构 创建侧边导航栏的基本结构,通常使用 <ul&…

vue实现右滑

vue实现右滑

Vue 实现右滑功能 右滑功能通常用于移动端应用,实现手势操作。以下是几种常见的实现方法: 使用 Hammer.js 库 Hammer.js 是一个专门处理手势的库,可以方便地实现右滑功能。…