当前位置:首页 > 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 示例:

<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
    }
  }
})

组件中使用:

<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 状态,避免手动控制。

vue 实现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 的 v-for 和 setInterval 实现基础轮播效果: <template> <div class="keyword…

vue实现bootstrap

vue实现bootstrap

Vue 中集成 Bootstrap 的方法 在 Vue 项目中集成 Bootstrap 可以通过以下方式实现,涵盖样式、组件和交互功能。 安装 Bootstrap 依赖 通过 npm 或 yarn…

vue实现123456

vue实现123456

Vue 实现数字 123456 的显示 在 Vue 中显示数字 123456 非常简单,可以通过以下方法实现: 方法一:直接在模板中显示 <template> <div>…

vue实现气泡

vue实现气泡

Vue 实现气泡效果的方法 在 Vue 中实现气泡效果可以通过 CSS 动画、第三方库或自定义组件完成。以下是几种常见实现方式: 使用纯 CSS 和 Vue 过渡 通过 Vue 的过渡系统结合 CS…

vue实现登录退出

vue实现登录退出

实现登录功能 在Vue中实现登录功能通常需要结合表单验证、API请求和状态管理。以下是一个基础实现示例: 安装必要依赖(如axios和vuex): npm install axios vuex 创…

vue实现部门树

vue实现部门树

Vue 实现部门树的方法 使用递归组件实现树形结构 递归组件是 Vue 中实现树形结构的常见方式。通过组件自身调用自身,可以轻松构建多层级的部门树。 <template> <d…