当前位置:首页 > VUE

vue 实现loading

2026-03-07 11:38:10VUE

Vue 实现 Loading 效果的方法

使用第三方库(如 Element UI、Ant Design Vue)

安装对应的 UI 库(以 Element UI 为例):

npm install element-ui

在 Vue 项目中引入并使用 Loading 组件:

import Vue from 'vue';
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';

Vue.use(ElementUI);

// 在组件中调用 Loading
this.$loading({
  lock: true,
  text: '加载中',
  spinner: 'el-icon-loading',
  background: 'rgba(0, 0, 0, 0.7)'
});

// 关闭 Loading
const loadingInstance = this.$loading();
loadingInstance.close();

自定义全局 Loading 组件

创建一个全局 Loading 组件(如 Loading.vue):

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

<script>
export default {
  props: {
    visible: Boolean,
    text: {
      type: String,
      default: '加载中...'
    }
  }
};
</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>

main.js 中注册为全局组件:

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

Vue.component('Loading', Loading);

在需要的地方使用:

<template>
  <div>
    <Loading :visible="isLoading" text="数据加载中..." />
  </div>
</template>

<script>
export default {
  data() {
    return {
      isLoading: false
    };
  },
  methods: {
    fetchData() {
      this.isLoading = true;
      // 模拟异步请求
      setTimeout(() => {
        this.isLoading = false;
      }, 2000);
    }
  }
};
</script>

使用 Vuex 管理全局 Loading 状态

在 Vuex 中定义状态:

// store.js
export default new Vuex.Store({
  state: {
    isLoading: false
  },
  mutations: {
    setLoading(state, isLoading) {
      state.isLoading = isLoading;
    }
  }
});

在根组件中使用:

<template>
  <div id="app">
    <Loading :visible="$store.state.isLoading" />
    <router-view />
  </div>
</template>

在需要的地方调用:

this.$store.commit('setLoading', true);
// 异步操作完成后
this.$store.commit('setLoading', false);

使用路由守卫实现页面切换 Loading

在路由配置中添加全局守卫:

vue 实现loading

// router.js
router.beforeEach((to, from, next) => {
  store.commit('setLoading', true);
  next();
});

router.afterEach(() => {
  setTimeout(() => {
    store.commit('setLoading', false);
  }, 500);
});

标签: vueloading
分享给朋友:

相关文章

vue实现波形

vue实现波形

实现波形效果的方法 在Vue中实现波形效果可以通过多种方式完成,常见的方法包括使用Canvas绘制、CSS动画或第三方库。以下是几种实现方案: 使用Canvas绘制波形 通过Canvas API动态…

vue实现快手

vue实现快手

Vue 实现类似快手的功能 Vue.js 是一个渐进式 JavaScript 框架,适合构建复杂的单页应用。要实现类似快手的功能,可以结合 Vue 和相关技术栈进行开发。 核心功能模块 视频流展示…

vue实现dag

vue实现dag

Vue实现DAG(有向无环图) 在Vue中实现DAG(Directed Acyclic Graph,有向无环图)通常涉及数据结构的建模、可视化渲染以及交互逻辑处理。以下是关键实现步骤和示例代码: 数…

vue实现github

vue实现github

Vue 实现 GitHub 相关功能 创建 GitHub API 请求 在 Vue 项目中,可以使用 axios 或 fetch 与 GitHub API 进行交互。需要在 GitHub 上创建个人访…

vue实现tabbar

vue实现tabbar

Vue 实现 TabBar 的方法 使用 Vue Router 实现基础 TabBar 安装 Vue Router 依赖: npm install vue-router 创建路由配置文件(route…

vue 实现页面返回

vue 实现页面返回

实现页面返回的方法 在Vue中实现页面返回功能可以通过多种方式实现,以下是几种常见的方案: 使用Vue Router的go方法 通过Vue Router的go方法可以控制浏览器的历史记录导航。rou…