Home > Web Front-end > Vue.js > How to use Vue3 asynchronous component Suspense

How to use Vue3 asynchronous component Suspense

王林
Release: 2023-05-14 12:07:06
forward
1679 people have browsed it

Suspense component

The official website mentions that it is an experimental function:
<suspense></suspense> is an experimental function. It won't necessarily end up as a stable feature, and the API may change before it becomes stable.
<suspense></suspense> is a built-in component used to coordinate the processing of asynchronous dependencies in the component tree. It allows us to wait higher up the component tree for multiple nested asynchronous dependencies below to be resolved, and render a loading state while waiting.

It means that this component is used to render some additional content while waiting for asynchronous components, so that the application has a better user experience

To understand <suspense> </suspense> To solve the problem and how it interacts with asynchronous dependencies, we need to imagine such a component hierarchy:

<Suspense>
└─ <Dashboard>
   ├─ <Profile>
   │  └─ <FriendStatus>(组件有异步的 setup())
   └─ <Content>
      ├─ <ActivityFeed> (异步组件)
      └─ <Stats>(异步组件)
Copy after login

There are multiple nested components in this component tree. To render They first have to parse some asynchronous resources. Without <Suspense>, they each need to handle their own loading, error reporting, and completion status. In the worst-case scenario, we might see three rotating loading states on the page, showing content at different times.

With the <Suspense> component, we can display the loading or failed loading status at the top level while waiting for the results of each asynchronous dependency in the entire multi-level component tree. state.

Let’s look at a simple example:

First we need to introduce asynchronous components

import {defineAsyncComponent} from &#39;vue&#39;
const Child = defineAsyncComponent(()=>import(&#39;./components/Child.vue&#39;))
Copy after login

To be simpler, we can use components to achieve the effect of asynchronous loading

Home parent component code is as follows:

<template>
  <div class="home">
    <h4>我是Home组件</h4>
    <Suspense>
       <template #default>
        <Child />
      </template>
      <template v-slot:fallback>
        <h4>Loading...</h4>
      </template>
    </Suspense>
  </div>
</template>
 
<script >
// import Child from &#39;./components/Child&#39;//静态引入
import { defineAsyncComponent  } from "vue";
const Child = defineAsyncComponent(() => import("../components/Child"));
export default {
  name: "",
  components: { Child },
};
</script>
 
<style>
.home {
  width: 300px;
  background-color: gray;
  padding: 10px;
}
</style>
Copy after login

Self-component Child

<template>
  <div class="child">
    <h4>我是Child组件</h4>
    name: {{user.name}}
    age: {{user.age}}
  </div>
</template>

<script>
import { ref } from "vue";
export default {
  name: "Child",
  async setup() {
    const NanUser = () => {
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve({
            name: "NanChen",
            age: 20,
          });
        },2000);
      });
    };
    const user = await NanUser();
    return {
      user,
    };
  },
};
</script>

<style>
.child {
  background-color: skyblue;
  padding: 10px;
}
</style>
Copy after login

According to the slot mechanism, components are distinguished. #default The content in the slot is The asynchronous component you need to render; #fallback is the loading static component you specified.

The effect is as follows:
How to use Vue3 asynchronous component Suspense

The above is the detailed content of How to use Vue3 asynchronous component Suspense. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template