当前位置:首页 > VUE

vue.js实现loading

2026-01-20 12:41:22VUE

实现全局Loading组件

在Vue.js中可以通过自定义组件实现全局Loading效果。创建一个独立的Loading组件,通过Vue的插件机制全局注册。

创建Loading组件

<template>
  <div v-if="visible" class="loading-overlay">
    <div class="loading-spinner"></div>
    <div class="loading-text">{{ text || 'Loading...' }}</div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      visible: false,
      text: ''
    }
  },
  methods: {
    show(text) {
      this.text = text
      this.visible = true
    },
    hide() {
      this.visible = false
    }
  }
}
</script>

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

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

.loading-text {
  color: white;
  margin-top: 10px;
}

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

注册为Vue插件

将Loading组件注册为Vue插件,方便全局调用。

// loading-plugin.js
import Vue from 'vue'
import LoadingComponent from './Loading.vue'

const LoadingPlugin = {
  install(Vue) {
    const LoadingConstructor = Vue.extend(LoadingComponent)
    const loadingInstance = new LoadingConstructor()

    loadingInstance.$mount(document.createElement('div'))
    document.body.appendChild(loadingInstance.$el)

    Vue.prototype.$loading = {
      show(text) {
        loadingInstance.show(text)
      },
      hide() {
        loadingInstance.hide()
      }
    }
  }
}

Vue.use(LoadingPlugin)

在项目中使用

在main.js中引入插件后,可以在任何组件中调用。

// 显示loading
this.$loading.show('加载中...')

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

结合axios拦截器实现自动Loading

可以在请求拦截器中自动显示Loading,响应拦截器中隐藏。

import axios from 'axios'

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

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

使用第三方库实现Loading

Vue生态中有许多成熟的Loading组件库,如Element UI、Vuetify等。

Element UI示例

import { Loading } from 'element-ui'

// 开启loading
let loadingInstance = Loading.service({
  lock: true,
  text: '加载中',
  spinner: 'el-icon-loading',
  background: 'rgba(0, 0, 0, 0.7)'
})

// 关闭loading
loadingInstance.close()

局部Loading实现

在单个组件内部实现Loading效果,适用于局部内容加载。

vue.js实现loading

<template>
  <div>
    <div v-if="loading" class="local-loading">加载中...</div>
    <div v-else>内容已加载</div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      loading: true
    }
  },
  mounted() {
    setTimeout(() => {
      this.loading = false
    }, 2000)
  }
}
</script>

标签: vuejs
分享给朋友:

相关文章

vue前端实现注册

vue前端实现注册

使用 Vue 实现用户注册功能 环境准备 确保已安装 Vue CLI 或通过 CDN 引入 Vue。若使用 Vue CLI,需安装 axios 或其他 HTTP 库用于后端通信。 注册表单组件 创建…

vue实现录播播放

vue实现录播播放

Vue 实现录播播放 使用 video.js 实现 安装 video.js 和相关的 Vue 适配器: npm install video.js @videojs-player/vue 在 Vue…

vue实现全局遮罩层

vue实现全局遮罩层

实现全局遮罩层的方法 在Vue中实现全局遮罩层可以通过多种方式完成,以下是几种常见的实现方法: 使用Vue组件创建遮罩层 创建一个遮罩层组件,通过全局注册或动态挂载的方式实现全局调用。以下是一个简单…

vue实现画圆弧并着色

vue实现画圆弧并着色

在 Vue 中实现画圆弧并着色 使用 Canvas API 绘制圆弧 在 Vue 组件的 mounted 钩子中,通过 Canvas API 绘制圆弧。创建一个 canvas 元素并获取其上下文:…

vue前端实现下载进度

vue前端实现下载进度

实现下载进度条的基本思路 在Vue中实现下载进度条,通常需要结合XMLHttpRequest或Fetch API来监听下载进度事件。通过计算已下载数据与总数据的比例,动态更新进度条的显示。 使用XM…

vue 实现递归

vue 实现递归

Vue 实现递归组件的方法 在 Vue 中实现递归组件通常用于处理树形结构数据,例如菜单、评论列表或文件目录。以下是实现递归组件的关键步骤: 定义递归组件 递归组件需要设置 name 属性,以便在模…