当前位置:首页 > VUE

vue实现loading插件

2026-01-19 08:45:25VUE

Vue 实现 Loading 插件的方法

创建基础组件

components 目录下创建 Loading.vue 文件,定义加载动画和样式。例如:

<template>
  <div class="loading-overlay" v-if="isVisible">
    <div class="loading-spinner"></div>
    <p class="loading-text">{{ text }}</p>
  </div>
</template>

<script>
export default {
  props: {
    isVisible: Boolean,
    text: {
      type: String,
      default: 'Loading...'
    }
  }
};
</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;
  justify-content: center;
  align-items: 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;
}
@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}
</style>

封装插件逻辑

plugins 目录下创建 loading.js,通过 Vue 的插件机制扩展全局功能:

vue实现loading插件

import Vue from 'vue';
import LoadingComponent from '@/components/Loading.vue';

const LoadingPlugin = {
  install(Vue) {
    const LoadingConstructor = Vue.extend(LoadingComponent);
    let loadingInstance = null;

    Vue.prototype.$loading = {
      show(text = 'Loading...') {
        if (!loadingInstance) {
          loadingInstance = new LoadingConstructor({
            propsData: { isVisible: true, text }
          }).$mount();
          document.body.appendChild(loadingInstance.$el);
        }
      },
      hide() {
        if (loadingInstance) {
          document.body.removeChild(loadingInstance.$el);
          loadingInstance.$destroy();
          loadingInstance = null;
        }
      }
    };
  }
};

Vue.use(LoadingPlugin);

全局注册插件

main.js 中引入并注册插件:

import LoadingPlugin from '@/plugins/loading';
Vue.use(LoadingPlugin);

使用插件

在组件或请求拦截器中调用:

vue实现loading插件

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

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

结合 Axios 拦截器

在请求拦截器中自动显示/隐藏 Loading:

import axios from 'axios';

let loadingCount = 0;

axios.interceptors.request.use(config => {
  if (loadingCount === 0) {
    Vue.prototype.$loading.show();
  }
  loadingCount++;
  return config;
});

axios.interceptors.response.use(
  response => {
    loadingCount--;
    if (loadingCount === 0) {
      Vue.prototype.$loading.hide();
    }
    return response;
  },
  error => {
    loadingCount--;
    if (loadingCount === 0) {
      Vue.prototype.$loading.hide();
    }
    return Promise.reject(error);
  }
);

可选配置扩展

支持自定义颜色、大小等配置:

Vue.prototype.$loading = {
  show(options = {}) {
    const defaults = { text: 'Loading...', color: '#3498db', size: 40 };
    const config = { ...defaults, ...options };
    // 应用配置到组件实例
  }
};

通过以上方法可以实现一个功能完善的 Vue Loading 插件,支持全局调用和自定义配置。

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

相关文章

vue状态管理怎么实现

vue状态管理怎么实现

Vue 状态管理实现方法 使用 Vuex(官方推荐) Vuex 是 Vue 的官方状态管理库,适合中大型应用。 安装 Vuex: npm install vuex --save 创建 Store 示…

vue实现飘窗原理

vue实现飘窗原理

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

vue实现菜单栏锚点

vue实现菜单栏锚点

实现锚点菜单的基本思路 在Vue中实现菜单栏锚点功能,主要涉及两个方面:创建可跳转的锚点位置,以及菜单项的点击滚动控制。通过监听滚动事件可以高亮当前可见区域的对应菜单项。 创建页面锚点位置 在需要…

vue实现一个标签选择

vue实现一个标签选择

Vue 标签选择组件实现 基础实现 使用 Vue 的 v-for 和 v-model 指令可以快速实现标签选择功能: <template> <div class="tag-se…

vue实现打印二维码

vue实现打印二维码

安装二维码生成库 使用 qrcode 或 vue-qrcode 库生成二维码。通过 npm 安装: npm install qrcode vue-qrcode 生成二维码 在 Vue 组…

vue原型实现

vue原型实现

Vue 原型实现的关键点 Vue.js 的原型系统是其响应式机制的核心部分,通过原型链实现数据的动态绑定和依赖收集。以下是实现 Vue 原型的主要技术要点。 数据响应式原理 Vue 通过 Objec…