Home Web Front-end uni-app How to customize loading globally in uniapp

How to customize loading globally in uniapp

May 22, 2023 am 09:54 AM

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:

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

  1. 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>
Copy after login

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.

  1. 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>
Copy after login

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.

  1. 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组件
      }
    }
  }
}
Copy after login

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What are the different types of testing that you can perform in a UniApp application? What are the different types of testing that you can perform in a UniApp application? Mar 27, 2025 pm 04:59 PM

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

What debugging tools are available for UniApp development? What debugging tools are available for UniApp development? Mar 27, 2025 pm 05:05 PM

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

How can you reduce the size of your UniApp application package? How can you reduce the size of your UniApp application package? Mar 27, 2025 pm 04:45 PM

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

How can you optimize images for web performance in UniApp? How can you optimize images for web performance in UniApp? Mar 27, 2025 pm 04:50 PM

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

How can you use lazy loading to improve performance? How can you use lazy loading to improve performance? Mar 27, 2025 pm 04:47 PM

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.

What are some common patterns for managing complex data structures in UniApp? What are some common patterns for managing complex data structures in UniApp? Mar 25, 2025 pm 02:31 PM

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.

What are computed properties in UniApp? How are they used? What are computed properties in UniApp? How are they used? Mar 25, 2025 pm 02:23 PM

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

How does UniApp handle global configuration and styling? How does UniApp handle global configuration and styling? Mar 25, 2025 pm 02:20 PM

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.

See all articles