当前位置:首页 > 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实现项目的关键方法 搭建基础环境 安装Node.js和npm/yarn,确保开发环境具备运行Vue的能力。通过Vue CLI快速初始化项目结构,选择所需配置(如Babel、Router、Vu…

实现vue

实现vue

安装Vue.js 通过npm安装Vue.js是最常见的方式。确保已安装Node.js和npm,运行以下命令: npm install vue 或者使用CDN直接引入: <script src…

vue 实现直播

vue 实现直播

Vue 实现直播的基本方法 在Vue中实现直播功能,通常需要结合WebRTC、RTMP或HLS等技术。以下是几种常见的实现方式: 使用WebRTC实现实时直播 WebRTC适合低延迟的实时直播场景,…

vue课程实现

vue课程实现

Vue 课程实现方案 课程内容设计 Vue课程应从基础到高级逐步展开,涵盖Vue核心概念、组件化开发、状态管理、路由、构建工具等。基础部分包括Vue实例、模板语法、计算属性和侦听器。中级部分深入组件通…

vue实现试卷

vue实现试卷

Vue实现试卷系统的基本思路 使用Vue.js构建试卷系统需要结合组件化开发、状态管理和后端数据交互。核心功能包括题目展示、答题逻辑、计时器和提交答案。 核心功能模块划分 试卷组件结构 试卷容器组…

vue实现博客

vue实现博客

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