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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

How do I handle local storage in uni-app? How do I handle local storage in uni-app? Mar 11, 2025 pm 07:12 PM

This article details uni-app's local storage APIs (uni.setStorageSync(), uni.getStorageSync(), and their async counterparts), emphasizing best practices like using descriptive keys, limiting data size, and handling JSON parsing. It stresses that lo

How to rename UniApp download files How to rename UniApp download files Mar 04, 2025 pm 03:43 PM

This article details workarounds for renaming downloaded files in UniApp, lacking direct API support. Android/iOS require native plugins for post-download renaming, while H5 solutions are limited to suggesting filenames. The process involves tempor

How to handle file encoding with UniApp download How to handle file encoding with UniApp download Mar 04, 2025 pm 03:32 PM

This article addresses file encoding issues in UniApp downloads. It emphasizes the importance of server-side Content-Type headers and using JavaScript's TextDecoder for client-side decoding based on these headers. Solutions for common encoding prob

How do I use uni-app's geolocation APIs? How do I use uni-app's geolocation APIs? Mar 11, 2025 pm 07:14 PM

This article details uni-app's geolocation APIs, focusing on uni.getLocation(). It addresses common pitfalls like incorrect coordinate systems (gcj02 vs. wgs84) and permission issues. Improving location accuracy via averaging readings and handling

How do I make API requests and handle data in uni-app? How do I make API requests and handle data in uni-app? Mar 11, 2025 pm 07:09 PM

This article details making and securing API requests within uni-app using uni.request or Axios. It covers handling JSON responses, best security practices (HTTPS, authentication, input validation), troubleshooting failures (network issues, CORS, s

How do I manage state in uni-app using Vuex or Pinia? How do I manage state in uni-app using Vuex or Pinia? Mar 11, 2025 pm 07:08 PM

This article compares Vuex and Pinia for state management in uni-app. It details their features, implementation, and best practices, highlighting Pinia's simplicity versus Vuex's structure. The choice depends on project complexity, with Pinia suita

How do I use uni-app's social sharing APIs? How do I use uni-app's social sharing APIs? Mar 13, 2025 pm 06:30 PM

The article details how to integrate social sharing into uni-app projects using uni.share API, covering setup, configuration, and testing across platforms like WeChat and Weibo.

How do I use uni-app's easycom feature for automatic component registration? How do I use uni-app's easycom feature for automatic component registration? Mar 11, 2025 pm 07:11 PM

This article explains uni-app's easycom feature, automating component registration. It details configuration, including autoscan and custom component mapping, highlighting benefits like reduced boilerplate, improved speed, and enhanced readability.

See all articles