当前位置:首页 > VUE

vue 怎么实现loading

2026-02-18 09:56:21VUE

实现全局 Loading 的方法

在 Vue 中实现全局 Loading 效果,可以通过以下几种方式:

使用 Vuex 管理状态

通过 Vuex 存储全局 Loading 状态,在需要时切换显示/隐藏。

// store.js
const store = new Vuex.Store({
  state: {
    isLoading: false
  },
  mutations: {
    setLoading(state, isLoading) {
      state.isLoading = isLoading
    }
  }
})
<!-- App.vue -->
<template>
  <div>
    <div v-if="$store.state.isLoading" class="loading-overlay">
      <div class="loading-spinner"></div>
    </div>
    <router-view />
  </div>
</template>

使用拦截器(Axios)

在 Axios 请求拦截器中自动控制 Loading 状态。

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

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 插件,通过 Vue.prototypeprovide/inject 方式调用。

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

局部 Loading 的实现

在单个组件中实现 Loading 效果,可以结合 v-if 或动态类名控制。

<template>
  <div>
    <button @click="fetchData" :disabled="isLoading">
      {{ isLoading ? 'Loading...' : 'Load Data' }}
    </button>
    <div v-if="isLoading" class="loading-indicator">
      Loading...
    </div>
  </div>
</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>

使用第三方库

常见的第三方库(如 Element UIAnt Design Vue)提供现成的 Loading 组件。

<template>
  <div>
    <el-button @click="showLoading">Show Loading</el-button>
  </div>
</template>

<script>
export default {
  methods: {
    showLoading() {
      const loading = this.$loading({
        lock: true,
        text: 'Loading',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      })
      setTimeout(() => loading.close(), 2000)
    }
  }
}
</script>

CSS 动画优化

为 Loading 添加平滑动画提升用户体验。

vue 怎么实现loading

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

@keyframes spin {
  from { transform: rotate(0deg); }
  to { transform: rotate(360deg); }
}

通过以上方法,可以灵活实现全局或局部 Loading 效果,结合项目需求选择合适方案。

标签: vueloading
分享给朋友:

相关文章

vue实现绑卡的原理

vue实现绑卡的原理

Vue 实现绑卡的原理 Vue 实现绑卡功能通常涉及前端表单处理、数据绑定、验证以及和后端 API 的交互。以下是实现绑卡功能的核心原理和步骤: 数据绑定与表单处理 Vue 通过 v-model 指…

vue实现fadein

vue实现fadein

Vue 实现 FadeIn 效果 在 Vue 中实现淡入(FadeIn)效果可以通过 CSS 过渡、动画或第三方库实现。以下是几种常见方法: 使用 CSS 过渡 通过 Vue 的过渡系统结合 CSS…

vue实现driver

vue实现driver

Vue 实现 Driver.js 引导功能 Driver.js 是一个轻量级的 JavaScript 库,用于在网页上创建引导式导览。以下是在 Vue 项目中集成 Driver.js 的详细方法:…

vue实现mui

vue实现mui

使用 Vue 实现 MUI(Material-UI)风格的界面 Vue 本身不直接提供 MUI(Material Design)组件,但可以通过集成第三方库或手动实现来达到类似效果。以下是几种常见方法…

vue实现datalist

vue实现datalist

使用 Vue 实现 HTML5 的 datalist 功能 HTML5 的 <datalist> 元素提供了一种自动完成输入框的功能,允许用户在输入时从预定义的选项列表中选择。以下是几种在…

vue实现定位打卡

vue实现定位打卡

Vue 实现定位打卡功能 获取用户地理位置 使用浏览器提供的 Geolocation API 获取用户当前位置坐标。在 Vue 组件中可以通过 navigator.geolocation 调用。 m…