当前位置:首页 > 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 组件是 Vue.js 的核心特性之一,允许将 UI 拆分为独立、可复用的模块。组件的实现包括定义、注册、数据传递和生命周期管理等。 定义组件 组件可以通过单文件组件(.…

vue router 实现

vue router 实现

Vue Router 的实现 Vue Router 是 Vue.js 的官方路由管理器,用于构建单页面应用(SPA)。以下是 Vue Router 的核心实现方式。 安装 Vue Router 通过…

vue 实现折叠

vue 实现折叠

Vue 实现折叠功能的方法 在 Vue 中实现折叠功能可以通过多种方式完成,以下是几种常见的实现方法: 使用 v-if 或 v-show 控制显示隐藏 通过绑定一个布尔值变量,结合 v-if 或 v…

lua 实现vue

lua 实现vue

在 Lua 中实现类似 Vue 的响应式框架需要模拟 Vue 的核心功能,包括数据绑定、模板渲染和依赖追踪。以下是实现的关键思路和代码示例: 响应式数据绑定 使用 Lua 的 __index 和 _…

vue框架实现CRM

vue框架实现CRM

Vue框架实现CRM系统 使用Vue框架构建CRM(客户关系管理)系统需要结合前端技术栈和后端API。以下为关键实现方法: 技术选型 核心框架:Vue 3(Composition API) UI组件…

用vue实现滚动

用vue实现滚动

实现滚动的基本方法 在Vue中实现滚动可以通过多种方式完成,包括使用原生JavaScript、Vue指令或第三方库。以下是几种常见的方法: 使用ref和原生JavaScript 在模板中定义一个可滚…