当前位置:首页 > 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实现全局控制。

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

Vue.component('Loading', Loading)

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

结合axios拦截器

在请求拦截器中显示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组件

// 安装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 alert实现

vue alert实现

使用 Vue 实现 Alert 组件 在 Vue 中实现 Alert 组件可以通过自定义组件或结合第三方库完成。以下是几种常见方法: 自定义 Alert 组件 创建一个可复用的 Alert 组件,通…

vue实现颜色

vue实现颜色

Vue 实现颜色的方法 在 Vue 中实现颜色管理可以通过多种方式,以下是一些常见的方法: 使用 CSS 变量动态绑定颜色 在 Vue 组件的样式中定义 CSS 变量,并通过 Vue 的数据绑定动态…

vue 实现注册

vue 实现注册

Vue 实现注册功能 注册功能通常包括表单验证、数据提交和用户反馈。以下是实现注册功能的步骤: 1. 创建注册表单组件 在 Vue 项目中创建一个注册表单组件,包含用户名、邮箱、密码等输入字段。…

实现vue cli

实现vue cli

安装 Vue CLI 确保系统中已安装 Node.js(版本建议 12.x 或更高)。通过 npm 全局安装 Vue CLI: npm install -g @vue/cli 安装完成后验证版本:…

vue实现路径

vue实现路径

Vue 实现路径的方法 在 Vue 中实现路径管理通常涉及 Vue Router 的使用,以下是一些常见的实现方法: 安装 Vue Router 通过 npm 或 yarn 安装 Vue Route…

vue实现uuid

vue实现uuid

使用 uuid 库生成 UUID 在 Vue 项目中,可以使用 uuid 库来生成 UUID。安装依赖: npm install uuid 在 Vue 组件中使用: import { v4 as…