How to customize loading globally in uniapp
With the rapid development of mobile Internet, mobile applications have become an indispensable part of people's lives. In mobile application development, loading animation is particularly important. It can significantly improve the user experience and allow users to perceive application feedback faster. This article will introduce how to use uniapp to implement global custom loading animation and improve user experience.
1. Why you need to customize the loading animation
In an application, loading animation is a very common feedback method, which is generally divided into two situations:
- Waiting for interaction with the background: For example, when requesting the background interface, you need to wait for a certain period of time. At this time, we generally need a loading animation to remind the user that it is loading.
- The initial loading time is too long: For example, when an application is opened, it needs to wait for the initial loading of the application. At this time, we also need a loading animation to remind the user that the application is loading.
However, the default style loading animation often cannot meet our needs, and custom styles and animations are often needed to improve the user experience. Therefore, we need to customize the loading animation globally.
2. Implementation plan
In uniapp, you can implement a global custom loading animation by implementing a Loading component in App.vue. The principle is through communication between parent and child components. Realize the display and hiding of global loading animation.
- Create Loading component
Create a Loading folder under the src/components folder, and then create a Loading.vue file inside it to display the custom Defined loading animation effect.
The code is as follows:
<template> <div v-show="isShow" class="loading"> <img src="@/static/loading.gif" alt="loading" /> </div> </template> <script> export default { props: { isShow: { type: Boolean, default: false } } } </script> <style> .loading { position: fixed; top: 0; left: 0; width: 100%; height: 100%; display: flex; justify-content: center; align-items: center; background: rgba(0, 0, 0, 0.3); z-index: 999; } img { width: 60px; height: 60px; } </style>
In the above code, we created a div and set its style to display the loading animation. The isShow attribute is passed in through props and is used to determine whether the loading animation needs to be displayed.
- Introducing the Loading component in App.vue
In App.vue, we need to introduce the Loading component and control its display and hiding through v-show.
The code is as follows:
<template> <div> <Loading :isShow="isLoading" /> <router-view /> </div> </template> <script> import Loading from '@/components/Loading/Loading' export default { components: { Loading }, data() { return { isLoading: false } }, methods: { startLoading() { this.isLoading = true }, endLoading() { this.isLoading = false } }, mounted() { this.$bus.$on('startLoading', this.startLoading) this.$bus.$on('endLoading', this.endLoading) }, beforeDestroy() { this.$bus.$off('startLoading', this.startLoading) this.$bus.$off('endLoading', this.endLoading) } } </script>
We introduced the Loading component in App.vue and controlled its display and hiding through v-show. At the same time, we set the isLoading variable in data to control the display of the Loading component.
In the mounted life cycle, listen to events named startLoading and endLoading through $bus.$on. These two events are triggered where we need to use loading animation to notify the parent component to display or Hide the Loading component. Remove the listening function through $bus.$off in the beforeDestroy life cycle to avoid memory leaks.
- Trigger startLoading and endLoading events where loading animation is required
Where loading animation is required, we trigger startLoading and endLoading events through $bus.$emit , notify the Loading component in App.vue to show and hide.
For example, in an asynchronous request:
import axios from 'axios' export default { methods: { async fetchData() { try { this.$bus.$emit('startLoading') // 触发startLoading事件,显示Loading组件 const response = await axios.get('/api/data') // 这里是异步请求数据 console.log(response.data) } catch (error) { console.error(error) } finally { this.$bus.$emit('endLoading') // 触发endLoading事件,隐藏Loading组件 } } } }
In the above code, we triggered the startLoading event through $bus.$emit before asynchronously requesting data, which is used to display the Loading component and request After the end, the endLoading event is triggered to hide the Loading component.
Through the above three steps, we can implement a simple global custom loading animation.
3. Summary
In mobile application development, loading animation is a very important feedback mechanism. In uniapp, by customizing the global Loading component, we can easily implement custom loading animations and improve user experience.
This article mainly implements global custom loading animation through three steps. First, the Loading component is created to display the customized loading animation effect, and then the Loading component is introduced in App.vue, through v-show Control its display and hiding, and finally trigger the startLoading and endLoading events where loading animation is needed to notify the Loading component in App.vue to display or hide.
The above is the detailed content of How to customize loading globally in uniapp. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



The article discusses various testing types for UniApp applications, including unit, integration, functional, UI/UX, performance, cross-platform, and security testing. It also covers ensuring cross-platform compatibility and recommends tools like Jes

The article discusses debugging tools and best practices for UniApp development, focusing on tools like HBuilderX, WeChat Developer Tools, and Chrome DevTools.

The article discusses strategies to reduce UniApp package size, focusing on code optimization, resource management, and techniques like code splitting and lazy loading.

The article discusses optimizing images in UniApp for better web performance through compression, responsive design, lazy loading, caching, and using WebP format.

Lazy loading defers non-critical resources to improve site performance, reducing load times and data usage. Key practices include prioritizing critical content and using efficient APIs.

The article discusses managing complex data structures in UniApp, focusing on patterns like Singleton, Observer, Factory, and State, and strategies for handling data state changes using Vuex and Vue 3 Composition API.

UniApp's computed properties, derived from Vue.js, enhance development by providing reactive, reusable, and optimized data handling. They automatically update when dependencies change, offering performance benefits and simplifying state management co

UniApp manages global configuration via manifest.json and styling through app.vue or app.scss, using uni.scss for variables and mixins. Best practices include using SCSS, modular styles, and responsive design.
