当前位置:首页 > 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 3(Composition API)或 Vue 2(Options API)作为前端框架,搭配 Vue Router 实现路由管理,V…

vue实现modal

vue实现modal

Vue 实现 Modal 弹窗组件 使用 Vue 实现 Modal 弹窗组件可以通过多种方式,包括原生 Vue 组件、第三方库或自定义指令。以下是几种常见方法: 原生 Vue 组件实现 创建一个基础…

vue实现ios

vue实现ios

Vue 实现 iOS 风格应用 使用 Vue 实现 iOS 风格的应用,可以通过结合 UI 框架和自定义样式来达成目标。以下是具体方法和步骤: 选择 iOS 风格的 UI 框架 Vue 生态中有多…

vue实现监听

vue实现监听

监听数据变化 在Vue中,可以通过watch选项或$watch方法监听数据的变化。watch适用于组件选项内声明式监听,$watch适用于动态监听。 // 选项式API export default…

vue 实现后退

vue 实现后退

实现后退功能的方法 在Vue中实现后退功能通常需要结合浏览器历史记录API或Vue Router的导航方法。以下是几种常见的实现方式: 使用Vue Router的go方法 this.$router…

vue实现ide

vue实现ide

Vue 实现 IDE 的基本思路 使用 Vue 构建一个简易的集成开发环境(IDE)需要整合代码编辑器、文件管理、终端模拟等功能。以下是关键实现步骤和推荐工具。 核心功能模块 代码编辑器 使用…