当前位置:首页 > 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 实现弹窗的常见方法 使用组件化方式封装弹窗 创建一个独立的弹窗组件(如 Modal.vue),通过 v-if 或 v-show 控制显示隐藏: <template> <…

代码实现vue

代码实现vue

创建Vue项目 使用Vue CLI快速初始化项目,确保已安装Node.js和npm/yarn: npm install -g @vue/cli vue create my-vue-project c…

vue页面实现

vue页面实现

Vue 页面实现方法 创建 Vue 组件 使用 Vue 单文件组件(SFC)方式创建页面,文件扩展名为 .vue。一个基本的 Vue 组件包含三个部分:模板(template)、脚本(script)和…

vue compile 实现

vue compile 实现

Vue 编译实现原理 Vue 的编译过程将模板字符串转换为渲染函数,主要分为解析、优化和代码生成三个阶段。 解析阶段(Parse) 将模板字符串转换为抽象语法树(AST)。Vue 使用正则表达式和有…

vue mvvm 实现

vue mvvm 实现

Vue MVVM 实现原理 Vue.js 的 MVVM(Model-View-ViewModel)架构通过数据绑定和响应式系统实现视图与数据的自动同步。以下是其核心实现机制: 数据劫持与响应式系统…

vue 实现vshow

vue 实现vshow

Vue 实现 v-show 在 Vue 中,v-show 是一个常用的指令,用于根据条件控制元素的显示或隐藏。与 v-if 不同,v-show 不会移除 DOM 元素,而是通过 CSS 的 displ…