How to use Vue3 asynchronous data loading component suspense

How to use Vue3 asynchronous data loading component suspense

Preface

Vue3 has added many eye-catching features, and the suspense component is one of them. It is very practical for processing asynchronous request data. This article introduces its usage through simple examples. If you are interested in it, you can refer to the official documentation.

It's common for components to need to perform some kind of asynchronous request before they can render correctly, and this is usually handled by a mechanism that developers design to handle this. There are many good ways to do this.

For example, if you want to get data asynchronously from an API and want to display some information, such as a loading effect, when the response data is parsed, you can use the suspense component in Vue3 to perform such requirements.

Creating Components

Create a component and name it Peoples.vue. Its component code is as follows:

<template>
    <div v-for="(people, index) in peoples.results" :key="index">
        {{ people.name }} {{ people.birth_year }}
    </div>
</template>
<script>
import { ref } from "vue";
export default {
    name: "CyPeoples",
    async setup() {
        const peoples = ref(null);
        const headers = { "Content-Type": "application/json" };
        const fetchPeoples = await fetch("https://swapi.dev/api/people", {
            headers,
        });
        peoples.value = await fetchPeoples.json();
        return { peoples };
    },
};
</script>

A ref is introduced here to ensure the reactivity of the component state. So, according to the above code snippet, the movie data is fetched through an asynchronous API call.
For initiating HTTP requests in VUE projects, Axios is usually used. Here we try to use fetch.

Ok, now let's use suspense to display loading information in the application.

Modify the App.vue file to make its code as follows:

<template>
    <div>
        <h1>Star Wars Characters</h1>
    </div>
    <suspense>
        <template #default>
            <CyPeoples />
        </template>
        <template #fallback>
            <div>
                <h3>Data loading...</h3>
            </div>
        </template>
    </suspense>
</template>
<script>
import CyPeoples from "./components/Peoples.vue";
import { suspense } from "vue";
export default {
    name: "App",
    components:
        CyPeoples,
        suspense,
    },
};
</script>

From the code snippet above, the loading effect can be easily achieved using the suspense component, with #default as the initialization template component to display the UI after the asynchronous request is completed. The #fallback is the processing UI in the asynchronous request, which is the common loading effect.

Summarize

The suspense component is a new feature of Vue3 that simplifies the processing logic of asynchronous request UI.

This is the end of this article about Vue3 asynchronous data loading component suspense. For more related Vue3 asynchronous data loading component suspense content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Vue3.0+vite2 implements dynamic asynchronous component lazy loading
  • In-depth understanding of Vue dynamic components and asynchronous components
  • Vue implements on-demand component loading and asynchronous component functions
  • Detailed explanation of Vue dynamic components and asynchronous components
  • Detailed explanation of asynchronous components in Vue component development

<<:  Installation, configuration and uninstallation of MySQL 8.0 in Windows environment

>>:  VMware Workstation Pro installs Win10 pure version operating system

Recommend

Vue implements the method example of tab routing switching component

Preface This article introduces the use of vue-ro...

How to ensure that every page of WeChat Mini Program is logged in

Table of contents status quo Solution Further sol...

Detailed explanation of mysql backup and recovery

Preface: The previous articles introduced the usa...

How to create a trigger in MySQL

This article example shares the specific code for...

Detailed tutorial on installing mysql-8.0.20 under Linux

** Install mysql-8.0.20 under Linux ** Environmen...

Eight ways to implement communication in Vue

Table of contents 1. Component Communication 1. P...

Pure CSS to achieve automatic rotation effect of carousel banner

Without further ado, let’s get straight to the co...

The whole process of configuring hive metadata to MySQL

In the hive installation directory, enter the con...

Detailed Tutorial on Using xargs Command on Linux

Hello everyone, I am Liang Xu. When using Linux, ...

mysql code to implement sequence function

MySQL implements sequence function 1. Create a se...

Linux server quick uninstall and install node environment (easy to get started)

1. Uninstall npm first sudo npm uninstall npm -g ...

centos7.2 offline installation mysql5.7.18.tar.gz

Because of network isolation, MySQL cannot be ins...

Using docker command does not require sudo

Because the docker daemon needs to bind to the ho...

In-depth explanation of slots and filters in Vue

Table of contents Slots What are slots? Slot Cont...

Docker Nginx container production and deployment implementation method

Quick Start 1. Find the nginx image on Docker Hub...