Home  >  Article  >  Web Front-end  >  How to customize loading globally in uniapp

How to customize loading globally in uniapp

WBOY
WBOYOriginal
2023-05-22 09:54:082976browse

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>

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>

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组件
      }
    }
  }
}

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:
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