当前位置:首页 > 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 的插件机制扩展全局功能:

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);

使用插件

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

// 显示加载
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实现loading插件

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

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

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

相关文章

vue实现防抖节流

vue实现防抖节流

Vue 中实现防抖(Debounce) 防抖的核心是延迟执行函数,在连续触发时取消前一次的调用,只执行最后一次。适用于输入框搜索、窗口大小调整等场景。 方法1:使用 Lodash 的 _.debou…

vue 路由 实现

vue 路由 实现

Vue 路由实现方法 Vue Router 是 Vue.js 的官方路由管理器,用于构建单页面应用(SPA)。以下是实现 Vue 路由的详细步骤: 安装 Vue Router 通过 npm 或 ya…

vue实现文字

vue实现文字

Vue 中实现文字显示的方法 在 Vue 中实现文字显示可以通过多种方式,包括插值表达式、指令、组件等。以下是几种常见的实现方法: 插值表达式 使用双大括号 {{ }} 进行文本插值,这是 Vue…

vue cli 实现

vue cli 实现

Vue CLI 实现步骤 安装 Vue CLI 确保 Node.js 版本在 8.9 或以上。通过 npm 全局安装 Vue CLI: npm install -g @vue/cli 创建新项目 使…

vue实现ide

vue实现ide

Vue 实现 IDE 的基本思路 使用 Vue 构建一个简易的集成开发环境(IDE)需要整合代码编辑器、文件管理、终端模拟等功能。以下是关键实现步骤和推荐工具。 核心功能模块 代码编辑器 使用…

vue底部实现

vue底部实现

Vue 底部实现方法 在 Vue 项目中实现底部布局可以通过多种方式完成,以下是一些常见的方法: 使用固定定位 将底部元素固定在页面底部,适用于单页应用或需要始终显示的底部栏。 <temp…