当前位置:首页 > VUE

vue 实现loading

2026-02-10 11:47:29VUE

Vue 实现 Loading 的方法

使用 v-if 和 v-show 控制显示

在 Vue 中可以通过 v-ifv-show 控制 Loading 组件的显示与隐藏。定义一个布尔值变量 isLoading,在异步操作开始和结束时切换状态。

<template>
  <div>
    <div v-if="isLoading" class="loading">Loading...</div>
    <button @click="fetchData">Load Data</button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      isLoading: false
    }
  },
  methods: {
    async fetchData() {
      this.isLoading = true
      try {
        await this.$http.get('/api/data')
      } finally {
        this.isLoading = false
      }
    }
  }
}
</script>

<style>
.loading {
  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;
  color: white;
}
</style>

使用第三方库

可以使用第三方库如 vue-loading-overlayelement-ui 的 Loading 组件快速实现。

安装 vue-loading-overlay

npm install vue-loading-overlay

使用示例:

<template>
  <div>
    <loading :active.sync="isLoading" :can-cancel="false"></loading>
    <button @click="fetchData">Load Data</button>
  </div>
</template>

<script>
import Loading from 'vue-loading-overlay'
import 'vue-loading-overlay/dist/vue-loading.css'

export default {
  components: {
    Loading
  },
  data() {
    return {
      isLoading: false
    }
  },
  methods: {
    async fetchData() {
      this.isLoading = true
      try {
        await this.$http.get('/api/data')
      } finally {
        this.isLoading = false
      }
    }
  }
}
</script>

全局 Loading 状态管理

对于大型应用,可以通过 Vuex 管理全局 Loading 状态。

定义 Vuex store:

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

在组件中使用:

<template>
  <div>
    <div v-if="$store.state.isLoading" class="loading">Loading...</div>
    <button @click="fetchData">Load Data</button>
  </div>
</template>

<script>
export default {
  methods: {
    async fetchData() {
      this.$store.commit('setLoading', true)
      try {
        await this.$http.get('/api/data')
      } finally {
        this.$store.commit('setLoading', false)
      }
    }
  }
}
</script>

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

在路由切换时显示 Loading,可以通过路由守卫实现。

// router.js
import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

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

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

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

export default router

使用动画增强体验

为 Loading 添加动画效果可以提升用户体验。

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

<style>
.loading {
  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;
}

.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>

以上方法可以根据具体需求选择适合的实现方式。对于简单场景,使用 v-ifv-show 即可;对于复杂应用,建议使用状态管理或第三方库。

vue 实现loading

标签: vueloading
分享给朋友:

相关文章

vue实现简单的弹窗

vue实现简单的弹窗

使用 Vue 实现简单弹窗 组件基础结构 创建一个名为 Modal.vue 的组件文件,包含模板、脚本和样式部分: <template> <div class="mod…

vue实现store

vue实现store

Vue 实现 Store 在 Vue 中,可以通过 Vuex 或 Pinia 实现全局状态管理(Store)。以下是两种主流方案的实现方法。 使用 Vuex 实现 Store Vuex 是 Vue…

vue slot实现

vue slot实现

vue slot 的实现方法 在 Vue 中,slot 是一种内容分发机制,允许父组件向子组件传递模板内容。以下是几种常见的 slot 实现方式: 默认 slot 默认 slot 是最基础的 sl…

vue实现粘贴

vue实现粘贴

Vue 实现粘贴功能的方法 在 Vue 中实现粘贴功能通常涉及监听粘贴事件并处理剪贴板数据。以下是几种常见的实现方式: 监听原生粘贴事件 通过 @paste 指令或原生 addEventListen…

vue 实现注册

vue 实现注册

Vue 实现注册功能 注册功能通常包括表单验证、数据提交和用户反馈。以下是实现注册功能的步骤: 1. 创建注册表单组件 在 Vue 项目中创建一个注册表单组件,包含用户名、邮箱、密码等输入字段。…

vue实现上传

vue实现上传

Vue 文件上传实现方法 使用 Vue 实现文件上传可以通过原生 HTML5 的 <input type="file"> 元素结合 Vue 的事件处理和数据绑定来实现。 基础实现步骤…