当前位置:首页 > VUE

vue实现loading组件

2026-02-19 22:29:27VUE

实现基础Loading组件

创建一个简单的Vue组件,通过v-show或v-if控制显示状态。组件可以包含旋转动画或进度条等视觉元素。

<template>
  <div class="loading" v-show="isLoading">
    <div class="spinner"></div>
    <p>{{ message }}</p>
  </div>
</template>

<script>
export default {
  props: {
    isLoading: {
      type: Boolean,
      default: false
    },
    message: {
      type: String,
      default: 'Loading...'
    }
  }
}
</script>

<style>
.loading {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0,0,0,0.5);
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  z-index: 9999;
}

.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); }
}
</style>

全局注册与使用

将Loading组件注册为全局组件,便于在任何地方调用。可以通过Vue.prototype或provide/inject实现全局控制。

vue实现loading组件

// main.js
import Vue from 'vue'
import Loading from './components/Loading.vue'

Vue.component('Loading', Loading)

// 或者添加全局方法
Vue.prototype.$loading = {
  show() {
    // 显示逻辑
  },
  hide() {
    // 隐藏逻辑
  }
}

结合axios拦截器

在请求拦截器中显示Loading,响应拦截器中隐藏,实现自动化的请求状态提示。

vue实现loading组件

import axios from 'axios'

const instance = axios.create()

let loadingCount = 0

instance.interceptors.request.use(config => {
  loadingCount++
  if (loadingCount === 1) {
    store.commit('setLoading', true)
  }
  return config
})

instance.interceptors.response.use(
  response => {
    loadingCount--
    if (loadingCount === 0) {
      store.commit('setLoading', false)
    }
    return response
  },
  error => {
    loadingCount--
    if (loadingCount === 0) {
      store.commit('setLoading', false)
    }
    return Promise.reject(error)
  }
)

高级功能实现

添加进度条效果、自定义动画、延迟显示等功能,提升用户体验。

<template>
  <div class="loading-progress" v-show="isLoading">
    <div class="progress-bar" :style="{ width: progress + '%' }"></div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      progress: 0,
      interval: null
    }
  },
  props: {
    isLoading: Boolean
  },
  watch: {
    isLoading(newVal) {
      if (newVal) {
        this.startProgress()
      } else {
        this.finishProgress()
      }
    }
  },
  methods: {
    startProgress() {
      this.progress = 0
      this.interval = setInterval(() => {
        this.progress = Math.min(this.progress + 10, 90)
      }, 300)
    },
    finishProgress() {
      clearInterval(this.interval)
      this.progress = 100
      setTimeout(() => {
        this.progress = 0
      }, 300)
    }
  }
}
</script>

第三方库集成

使用现成的Vue加载组件库如vue-loading-overlay、vue-simple-spinner等,快速实现专业效果。

// 安装vue-loading-overlay
npm install vue-loading-overlay

// 使用示例
import Loading from 'vue-loading-overlay'
import 'vue-loading-overlay/dist/vue-loading.css'

Vue.use(Loading)

// 在组件中使用
this.$loading.show({
  canCancel: true,
  onCancel: this.onCancel
})

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

标签: 组件vue
分享给朋友:

相关文章

vue实现轮播

vue实现轮播

Vue 实现轮播的方法 使用第三方库(如 Swiper) 安装 Swiper 库: npm install swiper 在 Vue 组件中引入 Swiper: <template&…

vue实现选择本地文件

vue实现选择本地文件

实现文件选择功能 在Vue中实现本地文件选择可以通过HTML原生的<input type="file">元素实现。创建一个基本的文件选择组件: <template> &l…

在线脑图 vue 实现

在线脑图 vue 实现

在线脑图 Vue 实现方案 使用开源库实现 推荐使用 mind-elixir 或 jsmind 这类开源库,它们专为脑图设计且支持 Vue 集成。 安装依赖: npm install mind…

vue实现继承语法糖

vue实现继承语法糖

在Vue中实现继承可以通过多种方式模拟类继承的效果,以下是常见的几种方法: 使用mixins实现逻辑复用 通过Vue的mixins机制共享组件选项,达到类似继承的效果: const Bas…

vue实现飘窗原理

vue实现飘窗原理

vue实现飘窗原理 飘窗效果通常指在页面上悬浮显示的弹窗或通知,跟随页面滚动或固定在某个位置。Vue实现这一效果主要依赖组件化开发、CSS定位和动态数据绑定。 组件结构设计 创建一个独立的Vue组件…

vue实现右下角弹框

vue实现右下角弹框

实现右下角弹框的基本思路 在Vue中实现右下角弹框,通常需要结合CSS定位和Vue的组件化特性。弹框可以通过绝对定位固定在右下角,并通过Vue控制其显示与隐藏。 创建弹框组件 新建一个Vue组件(如…