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
Dynamically adding form items iview's dynamic...
Table of contents [See an example]: [The original...
This article example shares the specific code of ...
This article example shares the specific code of ...
This article shares with you the graphic tutorial...
This article shares the installation tutorial of ...
Web Server 1. The web server turns off unnecessar...
Table of contents Login business process Login fu...
To split a string into an array, you need to use ...
SQL fuzzy query statement The general fuzzy state...
introduction I used postgresql database for some ...
How to implement the paging function of MyBatis i...
This article example shares the specific code for...
Think about it: Why should css be placed in the h...
Get the mongo image sudo docker pull mongo Run th...