SoFunction
Updated on 2025-04-11

Vue3 Suspense implements elegant processing of asynchronous data loading

Vue 3 brings a series of exciting new features, one of which isSuspenseSuspenseIt is a feature in Vue 3 that handles asynchronous data loading, which allows for a better user experience when loading asynchronous data, while allowing developers to manage asynchronous operations more easily. This article will introduce in-depth information about Vue 3Suspense, and explain how to use it to handle asynchronous data loading.

What is Suspense

Suspenseis a feature used to handle asynchronous operations such as data loading, which was introduced in Vue 3 to improve the user experience. It allows you to display placeholder content before the asynchronous operation is complete to prevent blank pages or load indicators. Once the asynchronous operation is completed,SuspenseIt will automatically switch to actual content, providing a better user experience.

SuspenseMainly used in the following situations:

  • Asynchronous component loading: When your application needs to wait for the asynchronous component loading to complete before rendering the component, you can useSuspense
  • Data loading: You can use it when you need to wait for the asynchronous data to complete before rendering the component to avoid rendering blanks or loading indicators.Suspense

How to use Suspense

To useSuspense, you need to understand two main concepts:<Suspense>Components andv-slotinstruction.

1. <Suspense> component

<Suspense>The component is the root component in Vue 3, which is used to wrap content that may cause asynchronous loading. When the content of the wrapper contains asynchronous operations,<Suspense>Its fallback (placeholder content) will be displayed until the asynchronous operation is completed.

&lt;template&gt;
  &lt;Suspense&gt;
    &lt;template #default&gt;
      &lt;!-- Asynchronous loading content --&gt;
      &lt;AsyncComponent /&gt;
    &lt;/template&gt;

    &lt;template #fallback&gt;
      &lt;!-- Placeholder content --&gt;
      &lt;LoadingIndicator /&gt;
    &lt;/template&gt;
  &lt;/Suspense&gt;
&lt;/template&gt;

In the example above,<Suspense>Package an asynchronous loading component<AsyncComponent />, and also provides a placeholder content<LoadingIndicator />, so that it is displayed when the asynchronous operation is in progress.

2. v-slot directive

v-slotDirectives are used to specifyfallbackanddefaultContents of the slot. In the example above, we use#defaultand#fallbackTo specify the contents of the two slots.

Handle asynchronous component loading

SuspenseMost commonly used to handle asynchronous component loading. Vue 3 allows you to load components on demand to reduce the initial loading time of your application. Here is an example showing how to use itSuspenseHandling asynchronous component loading:

<template>
  <Suspense>
    <template #default>
      <AsyncComponent />
    </template>

    <template #fallback>
      <LoadingIndicator />
    </template>
  </Suspense>
</template>

<script>
import { defineAsyncComponent } from 'vue';

const AsyncComponent = defineAsyncComponent(() =>
  import('./')
);

export default {
  components: {
    AsyncComponent
  }
};
</script>

In the example above,AsyncComponentis a loaded component that usesdefineAsyncComponentFunction wrapper. When loading asynchronouslyAsyncComponenthour,SuspenseWill displayLoadingIndicator, until loading is complete.

Process data loading

SuspenseIt can also be used to handle asynchronous data loading. This is useful for waiting for data to load before rendering components before rendering them to avoid rendering blanks or loading indicators.

<template>
  <Suspense>
    <template #default>
      <UserData :user- />
    </template>

    <template #fallback>
      <LoadingIndicator />
    </template>
  </Suspense>
</template>

In the example above,UserDataComponent AcceptanceuserIdattributes and according touserIdLoad user data. When the data is loaded, it will display user information, but during the data is loaded,SuspenseWill displayLoadingIndicator

Error handling

SuspenseA method is also provided to deal with asynchronous operation failures. You can use it internally in asynchronous operationstry/catchto catch the error, and thencatchPass in block$suspendMethod

KnowSuspense. This will triggerfallbackdisplay.

&lt;template&gt;
  &lt;Suspense&gt;
    &lt;template #default&gt;
      &lt;AsyncDataComponent /&gt;
    &lt;/template&gt;

    &lt;template #fallback&gt;
      &lt;ErrorIndicator /&gt;
    &lt;/template&gt;
  &lt;/Suspense&gt;
&lt;/template&gt;

&lt;script&gt;
import { ref } from 'vue';

const AsyncDataComponent = {
  async setup() {
    try {
      // Asynchronously load data      const data = await fetchData();
      return { data };
    } catch (error) {
      //Catch errors and notify Suspense      const suspend = ref(error);
      throw suspend;
    }
  }
};
&lt;/script&gt;

In the example above,AsyncDataComponentusetry/catchCatch the error and passthrowPass the error toSuspense, thereby triggeringfallbackdisplay.

Summarize

Suspenseis a powerful feature in Vue 3 for elegantly handling asynchronous component loading and data loading. It allows for a better user experience when loading asynchronous operations, while allowing developers to manage asynchronous operations more easily. If you are building a Vue 3 application and need to handle asynchronous operations, it is highly recommended that you learn and useSuspense, to improve user experience and development efficiency.

This is the end of this article about Vue3 Suspense implementing elegant processing of asynchronous data loading. For more related content on Vue3 Suspense to handle asynchronous data loading, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!