当前位置:首页 > 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 状态。

拦截器示例:

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实现loading插件

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

相关文章

vue实现用户信息删除

vue实现用户信息删除

Vue 实现用户信息删除功能 在 Vue 中实现用户信息删除功能通常涉及前端界面交互和后端 API 调用。以下是实现步骤: 前端界面设计 创建删除按钮或操作项,通常使用 @click 事件触发删除…

vue实现主题

vue实现主题

Vue 实现主题的方法 使用 CSS 变量动态切换主题 定义全局 CSS 变量,通过修改这些变量实现主题切换。在 Vue 的根组件或全局样式中定义变量: :root { --primary-c…

vue实现级联

vue实现级联

Vue 实现级联选择器的方法 级联选择器(Cascader)是常见的表单组件,用于处理多层级联动数据选择(如省市区选择)。以下是几种实现方式: 基于 Element UI 的 el-cascader…

vue的实现

vue的实现

Vue 的实现原理 Vue 是一个渐进式 JavaScript 框架,其核心实现主要包括响应式系统、虚拟 DOM、模板编译和组件化机制。以下是 Vue 实现的主要技术细节。 响应式系统 Vue 的…

vue实现博客

vue实现博客

Vue 实现博客的基本步骤 使用 Vue.js 实现一个博客系统可以分为前端和后端两部分。以下是基于 Vue 的前端实现方案,后端可以选择 Node.js、Django 或其他框架。 项目初始化 使…

vue实现ajax

vue实现ajax

Vue 中实现 AJAX 的几种方法 在 Vue 中实现 AJAX 请求可以通过原生 JavaScript 的 XMLHttpRequest、第三方库如 axios 或 fetch API 来完成。以…