当前位置:首页 > VUE

vue实现loading插件

2026-02-20 01:07:54VUE

Vue 实现 Loading 插件的方法

使用全局组件与状态管理

创建一个全局的 Loading 组件,通过 Vuex 或 Pinia 管理其显示状态。在需要的地方通过状态控制显示或隐藏。

Loading 组件示例:

<template>
  <div v-if="isLoading" class="loading-overlay">
    <div class="loading-spinner"></div>
  </div>
</template>

<script>
export default {
  computed: {
    isLoading() {
      return this.$store.state.isLoading;
    }
  }
};
</script>

<style>
.loading-overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 9999;
}
.loading-spinner {
  border: 4px solid rgba(255, 255, 255, 0.3);
  border-radius: 50%;
  border-top: 4px solid #fff;
  width: 40px;
  height: 40px;
  animation: spin 1s linear infinite;
}
@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}
</style>

在 Vuex 中管理状态:

const store = new Vuex.Store({
  state: {
    isLoading: false
  },
  mutations: {
    setLoading(state, isLoading) {
      state.isLoading = isLoading;
    }
  }
});

使用插件形式封装

将 Loading 功能封装为 Vue 插件,便于全局调用。

插件实现:

const LoadingPlugin = {
  install(Vue) {
    const LoadingComponent = Vue.extend({
      template: '<div v-if="show" class="loading-overlay"><div class="loading-spinner"></div></div>',
      data() {
        return { show: false };
      }
    });

    let loadingInstance = null;
    Vue.prototype.$loading = {
      show() {
        if (!loadingInstance) {
          loadingInstance = new LoadingComponent({
            el: document.createElement('div')
          });
          document.body.appendChild(loadingInstance.$el);
        }
        loadingInstance.show = true;
      },
      hide() {
        if (loadingInstance) {
          loadingInstance.show = false;
        }
      }
    };
  }
};

Vue.use(LoadingPlugin);

调用方式:

this.$loading.show(); // 显示 loading
this.$loading.hide(); // 隐藏 loading

使用指令实现局部 Loading

通过自定义指令在元素上实现局部 Loading 效果。

指令实现:

Vue.directive('loading', {
  bind(el, binding) {
    const loadingDiv = document.createElement('div');
    loadingDiv.className = 'loading-spinner';
    el.style.position = 'relative';
    loadingDiv.style.position = 'absolute';
    loadingDiv.style.top = '50%';
    loadingDiv.style.left = '50%';
    loadingDiv.style.transform = 'translate(-50%, -50%)';
    el.loadingElement = loadingDiv;

    if (binding.value) {
      el.appendChild(loadingDiv);
    }
  },
  update(el, binding) {
    if (binding.value) {
      el.appendChild(el.loadingElement);
    } else {
      if (el.contains(el.loadingElement)) {
        el.removeChild(el.loadingElement);
      }
    }
  }
});

使用方式:

<template>
  <div v-loading="isLoading"></div>
</template>

结合异步请求拦截器

在 axios 拦截器中自动控制 Loading 状态。

拦截器示例:

vue实现loading插件

let loadingCount = 0;

axios.interceptors.request.use(config => {
  loadingCount++;
  if (loadingCount === 1) {
    store.commit('setLoading', true);
  }
  return config;
});

axios.interceptors.response.use(
  response => {
    loadingCount--;
    if (loadingCount === 0) {
      store.commit('setLoading', false);
    }
    return response;
  },
  error => {
    loadingCount--;
    if (loadingCount === 0) {
      store.commit('setLoading', false);
    }
    return Promise.reject(error);
  }
);

以上方法可根据项目需求选择或组合使用,实现灵活可控的 Loading 效果。

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

相关文章

vue实现无线滚动列表

vue实现无线滚动列表

无限滚动列表的实现方法 在Vue中实现无限滚动列表通常结合监听滚动事件和动态加载数据。以下是几种常见方法: 使用自定义指令 创建自定义指令v-infinite-scroll监听滚动事件:…

vue实现剪辑

vue实现剪辑

Vue 实现视频剪辑功能 在Vue中实现视频剪辑功能通常需要结合HTML5的<video>元素和第三方库(如FFmpeg.js或Video.js)。以下是几种常见的实现方法: 使用FFm…

vue实现pie

vue实现pie

Vue 实现饼图 在 Vue 中实现饼图通常可以通过第三方图表库(如 ECharts、Chart.js 或 Highcharts)来完成。以下是使用 ECharts 和 Chart.js 的两种常见方…

vue diff实现

vue diff实现

Vue Diff 算法实现原理 Vue 的虚拟 DOM Diff 算法(即 patch 过程)是其高效渲染的核心,主要通过对比新旧虚拟节点的差异,最小化 DOM 操作。以下是关键实现要点: 同层级比…

vue实现抽屉

vue实现抽屉

Vue 实现抽屉组件 抽屉组件是一种常见的 UI 模式,通常用于从屏幕边缘滑出内容。以下是几种实现抽屉的方法: 使用 Vue 原生实现 创建一个基本的抽屉组件,利用 Vue 的过渡和条件渲染功能。…

vue模版实现

vue模版实现

Vue 模板实现方法 Vue 模板是 Vue.js 的核心特性之一,用于声明式地将 DOM 绑定至底层 Vue 实例的数据。以下是几种常见的 Vue 模板实现方式: 单文件组件(SFC) 使用 .v…