当前位置:首页 > VUE

vue实现loading页面

2026-03-08 14:48:45VUE

实现全局Loading组件

components文件夹下创建Loading.vue文件:

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

<script>
export default {
  data() {
    return {
      isLoading: false
    }
  },
  methods: {
    show() {
      this.isLoading = true
    },
    hide() {
      this.isLoading = false
    }
  }
}
</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;
}

.spinner {
  width: 50px;
  height: 50px;
  border: 5px solid #f3f3f3;
  border-top: 5px solid #3498db;
  border-radius: 50%;
  animation: spin 1s linear infinite;
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}
</style>

注册全局Loading

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

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

Vue.component('Loading', Loading)

const loading = new Vue(Loading).$mount()
document.body.appendChild(loading.$el)

Vue.prototype.$loading = {
  show: () => loading.show(),
  hide: () => loading.hide()
}

new Vue({
  render: h => h(App)
}).$mount('#app')

在请求拦截器中使用

在axios拦截器中控制Loading显示:

import axios from 'axios'

axios.interceptors.request.use(config => {
  Vue.prototype.$loading.show()
  return config
}, error => {
  Vue.prototype.$loading.hide()
  return Promise.reject(error)
})

axios.interceptors.response.use(response => {
  Vue.prototype.$loading.hide()
  return response
}, error => {
  Vue.prototype.$loading.hide()
  return Promise.reject(error)
})

按需使用的示例

在组件中手动控制Loading:

export default {
  methods: {
    fetchData() {
      this.$loading.show()
      axios.get('/api/data')
        .then(response => {
          // 处理数据
        })
        .finally(() => {
          this.$loading.hide()
        })
    }
  }
}

路由切换时的Loading

在路由配置中添加导航守卫:

import Router from 'vue-router'

const router = new Router({
  routes: [...]
})

router.beforeEach((to, from, next) => {
  Vue.prototype.$loading.show()
  next()
})

router.afterEach(() => {
  setTimeout(() => {
    Vue.prototype.$loading.hide()
  }, 300)
})

export default router

优化加载体验

添加最小显示时间避免闪烁:

vue实现loading页面

let minLoadingTime = 500
let startTime = 0

Vue.prototype.$loading = {
  show: () => {
    startTime = Date.now()
    loading.show()
  },
  hide: () => {
    const elapsed = Date.now() - startTime
    const remaining = minLoadingTime - elapsed
    if (remaining > 0) {
      setTimeout(() => loading.hide(), remaining)
    } else {
      loading.hide()
    }
  }
}

标签: 页面vue
分享给朋友:

相关文章

vue 移动端实现签名

vue 移动端实现签名

Vue 移动端签名实现方法 在移动端实现签名功能,可以通过HTML5的Canvas结合触摸事件完成。以下是具体实现步骤: 使用Canvas绘制签名 安装vue-signature-pad插件或直接使…

vue实现边框

vue实现边框

Vue 实现边框的方法 在 Vue 中实现边框效果可以通过多种方式完成,包括内联样式、CSS 类绑定、动态样式以及使用第三方 UI 库。以下是几种常见的实现方法。 内联样式绑定 使用 Vue 的 :…

vue实现分屏

vue实现分屏

Vue 实现分屏的方法 分屏功能通常用于展示多个独立内容区域,以下是几种常见的实现方式: 使用 CSS Grid 布局 通过 CSS Grid 可以轻松创建分屏布局,在 Vue 组件中直接应用样式:…

ssh vue 实现

ssh vue 实现

使用SSH连接Vue项目部署的服务器 在Vue项目开发或部署过程中,可能需要通过SSH连接到远程服务器进行配置或维护。以下是实现SSH连接并部署Vue项目的关键步骤: 生成SSH密钥对 在本地终端运…

vue架构实现

vue架构实现

Vue 架构实现核心方案 Vue.js 的架构实现通常围绕模块化、组件化和状态管理展开。以下为常见实现方案: 基于单文件组件(SFC)的模块化 通过 .vue 文件组织模板、脚本和样式: <…

vue实现系统

vue实现系统

Vue 实现系统的核心方法 使用 Vue.js 构建系统时,需结合其响应式特性与组件化开发模式。以下为关键实现路径: 项目初始化与工程配置 通过 Vue CLI 或 Vite 创建项目骨架,推荐选择…