当前位置:首页 > VUE

vue 实现loading

2026-01-08 00:25:34VUE

Vue 实现 Loading 的方法

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

在 Vue 中可以通过 v-ifv-show 控制 loading 组件的显示与隐藏。v-if 会动态创建或销毁组件,而 v-show 仅切换 CSS 的 display 属性。

<template>
  <div>
    <div v-if="isLoading" class="loading">
      Loading...
    </div>
    <div v-else>
      Content loaded!
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      isLoading: true
    }
  },
  mounted() {
    setTimeout(() => {
      this.isLoading = false
    }, 2000)
  }
}
</script>

<style>
.loading {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100px;
  background: #f0f0f0;
}
</style>

使用第三方库(如 Element UI、Vant)

许多 UI 框架提供了内置的 loading 组件,例如 Element UI 的 el-loading 或 Vant 的 van-loading

Element UI 示例:

<template>
  <div v-loading="isLoading" element-loading-text="Loading...">
    Content here
  </div>
</template>

<script>
export default {
  data() {
    return {
      isLoading: true
    }
  },
  mounted() {
    setTimeout(() => {
      this.isLoading = false
    }, 2000)
  }
}
</script>

Vant 示例:

vue 实现loading

<template>
  <div>
    <van-loading v-if="isLoading" type="spinner" />
    <div v-else>
      Content loaded!
    </div>
  </div>
</template>

<script>
import { Loading } from 'vant'
export default {
  components: {
    [Loading.name]: Loading
  },
  data() {
    return {
      isLoading: true
    }
  },
  mounted() {
    setTimeout(() => {
      this.isLoading = false
    }, 2000)
  }
}
</script>

全局 Loading 状态管理

通过 Vuex 或 Provide/Inject 实现全局 loading 状态管理,适合多个组件共享 loading 状态。

Vuex 示例:

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

组件中使用:

vue 实现loading

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

<script>
export default {
  methods: {
    fetchData() {
      this.$store.commit('setLoading', true)
      setTimeout(() => {
        this.$store.commit('setLoading', false)
      }, 2000)
    }
  }
}
</script>

自定义 Loading 动画

可以通过 CSS 或 SVG 实现自定义 loading 动画,提升用户体验。

CSS Spinner 示例:

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

<style>
.spinner {
  width: 40px;
  height: 40px;
  border: 4px solid rgba(0, 0, 0, 0.1);
  border-radius: 50%;
  border-left-color: #09f;
  animation: spin 1s linear infinite;
}

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

异步请求拦截器

在 Axios 拦截器中统一处理 loading 状态,避免手动控制。

// axios 配置
axios.interceptors.request.use(config => {
  store.commit('setLoading', true)
  return config
})

axios.interceptors.response.use(response => {
  store.commit('setLoading', false)
  return response
}, error => {
  store.commit('setLoading', false)
  return Promise.reject(error)
})

总结

Vue 实现 loading 的方式多样,可以根据项目需求选择合适的方法。简单的项目可以使用 v-ifv-show,复杂项目推荐结合状态管理或拦截器统一处理。第三方库提供了开箱即用的解决方案,而自定义动画能提升用户体验。

标签: vueloading
分享给朋友:

相关文章

vue实现绑卡的原理

vue实现绑卡的原理

Vue 实现绑卡的原理 Vue 实现绑卡功能通常涉及前端表单处理、数据绑定、验证以及和后端 API 的交互。以下是实现绑卡功能的核心原理和步骤: 数据绑定与表单处理 Vue 通过 v-model…

vue 实现

vue 实现

Vue 实现的基本步骤 安装 Vue.js 可以通过 npm 或 CDN 引入。使用 npm 安装适合大型项目,CDN 适合快速原型开发。 npm install vue 在 HTML 文件中引入…

vue 实现jqslidedown

vue 实现jqslidedown

在 Vue 中实现类似 jQuery 的 slideDown 效果,可以通过 Vue 的过渡系统或 CSS 动画结合动态样式绑定来实现。以下是几种实现方式: 使用 Vue Transition…

vue实现xss

vue实现xss

XSS 攻击简介 XSS(跨站脚本攻击)是一种常见的安全漏洞,攻击者通过注入恶意脚本到网页中,当其他用户访问该页面时,脚本会在其浏览器中执行,可能导致数据泄露或会话劫持。 Vue 中的 XSS…

vue实现route

vue实现route

Vue 路由实现方法 Vue 中实现路由通常使用 vue-router 库,这是 Vue 官方推荐的路由管理器。以下是具体实现步骤: 安装 vue-router 通过 npm 或 yarn 安装…

vue轮询实现

vue轮询实现

Vue 轮询实现方法 在 Vue 中实现轮询可以通过以下几种方式: 使用 setInterval data() { return { pollInterval: null } },…