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

In-depth understanding of javascript prototype and prototype chain

Table of contents 1. What is a prototype? 2. Prot...

A brief discussion on JS regular RegExp object

Table of contents 1. RegExp object 2. Grammar 2.1...

Mysql join table and id auto-increment example analysis

How to write join If you use left join, is the ta...

Tutorial on installing and using virtualenv in Deepin

virtualenv is a tool for creating isolated Python...

In-depth understanding of MySQL self-connection and join association

1. MySQL self-connection MySQL sometimes needs to...

CSS HACK for IE6/IE7/IE8/IE9/FF (summary)

Since I installed the official version of IE8.0, ...

CentOS6.8 Chinese/English environment switching tutorial diagram

1. Introduction People who are not used to Englis...

A Deeper Look at the Differences Between Link and @import

There are three main ways to use CSS in a page: ad...

An IE crash bug

Copy code The code is as follows: <style type=...

Summary of common problems and application skills in MySQL

Preface In the daily development or maintenance o...

Notes on configuring multiple proxies using vue projects

In the development process of Vue project, for th...

Detailed explanation of the mechanism and implementation of accept lock in Nginx

Preface nginx uses a multi-process model. When a ...