PrefaceVue3 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 ComponentsCreate 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. 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. SummarizeThe 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:
|
<<: Installation, configuration and uninstallation of MySQL 8.0 in Windows environment
>>: VMware Workstation Pro installs Win10 pure version operating system
Table of contents 1. What is a prototype? 2. Prot...
Table of contents 1. RegExp object 2. Grammar 2.1...
How to write join If you use left join, is the ta...
1. Filter Example: <!DOCTYPE html> <html...
virtualenv is a tool for creating isolated Python...
1. MySQL self-connection MySQL sometimes needs to...
Since I installed the official version of IE8.0, ...
Run the command: glxinfo | grep rendering If the ...
1. Introduction People who are not used to Englis...
There are three main ways to use CSS in a page: ad...
Copy code The code is as follows: <style type=...
Installation introduction under Windows: Check ou...
Preface In the daily development or maintenance o...
In the development process of Vue project, for th...
Preface nginx uses a multi-process model. When a ...