当前位置:首页 > 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 实现选择分类的方法 使用 v-model 绑定选择值 在 Vue 中可以通过 v-model 指令实现双向数据绑定,适用于下拉选择框(<select>)。以下是一个基本示例:…

vue实现收起展开面板

vue实现收起展开面板

实现收起展开面板的方法 在Vue中实现收起展开面板功能可以通过多种方式完成,以下是几种常见的实现方法: 使用v-show或v-if控制显示隐藏 利用Vue的指令可以轻松实现面板的展开和收起:…

vue穿梭框组件实现

vue穿梭框组件实现

实现基础穿梭框组件 在Vue中实现穿梭框组件,可以使用<el-transfer>(Element UI)或手动实现。以下是手动实现的核心逻辑: <template> &l…

vue优秀实现

vue优秀实现

Vue.js 优秀实现方法 响应式数据管理 使用 Vue 的 ref 和 reactive 处理基础类型和对象类型数据,结合 computed 计算属性优化渲染性能。对于复杂状态,推荐 Pinia 替…

vue  select实现

vue select实现

Vue Select 实现方法 在 Vue 中实现下拉选择功能可以使用原生 <select> 标签或第三方库如 vue-select。以下是两种方法的详细说明: 原生 HTML Sele…

vue实现图册

vue实现图册

Vue实现图册功能 在Vue中实现图册功能通常涉及图片展示、切换、缩放等交互效果。以下是常见的实现方法和关键代码示例: 基础图册组件结构 使用Vue单文件组件构建基础结构: <templat…