Home > Web Front-end > Vue.js > body text

How to use the Suspense component in Vue 3 to achieve data loading transition effects

PHPz
Release: 2023-09-09 10:28:52
Original
1057 people have browsed it

如何利用Vue 3中的Suspense组件实现数据加载过渡效果

How to use the Suspense component in Vue 3 to achieve data loading transition effects

Introduction:
As the complexity of web applications increases, the data loading transition effect becomes meets an important user experience requirement. Vue 3 has made further improvements in this area and introduced the Suspense component to solve this problem. This article will introduce how to use the Suspense component in Vue 3 to achieve data loading transition effects, and attach corresponding code examples.

  1. Introducing the Suspense component
    The Suspense component in Vue 3 was introduced to solve the problem of data loading status display during the loading process of asynchronous components. We can wrap the asynchronous component by using the Suspense component in the template and display a loading state before the asynchronous component is loaded.
<template>
  <Suspense>
    <template #default>
      <AsyncComponent/>
    </template>
    <template #fallback>
      <loading-component/>
    </template>
  </Suspense>
</template>
Copy after login
  1. Define asynchronous components
    Asynchronous components can be dynamically loaded through the import function, and Vue will automatically convert them into asynchronous components. We can display a loading state during the loading process of an asynchronous component until the component is loaded.
const AsyncComponent = defineAsyncComponent(
  () => import('./AsyncComponent.vue'),
  {
    loadingComponent: loadingComponent,
    errorComponent: errorComponent,
    delay: 200, // 延迟200毫秒显示loading状态
    timeout: 3000 // 3秒超时时间
  }
);
Copy after login
  1. Custom loading status component
    loadingComponent and errorComponent are our custom components, which represent the status of data loading and loading failure respectively. We can customize the display effects of these two components according to actual needs. The following is a sample code for loadingComponent:
<template>
  <div class="loading">数据加载中...</div>
</template>

<script>
export default {
  name: 'loadingComponent'
}
</script>

<style scoped>
.loading {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100px;
  background-color: #f5f5f5;
}
</style>
Copy after login
  1. Display after the component is loaded
    When the component is loaded, we can display data in the asynchronous component. At this time, Vue will automatically replace the fallback template content of the Suspense component with the content of the asynchronous component.
<template>
  <div>
    <h1>{{title}}</h1>
    <p>{{content}}</p>
  </div>
</template>

<script>
export default {
  name: 'AsyncComponent',
  data() {
    return {
      title: '',
      content: ''
    }
  },
  created() {
    fetch('/api/data')
      .then(response => response.json())
      .then(data => {
        this.title = data.title;
        this.content = data.content;
      })
      .catch(error => {
        console.error(error);
      });
  }
}
</script>
Copy after login
  1. Complete sample code
    The following is a complete sample code that shows how to use the Suspense component in Vue 3 to achieve the data loading transition effect.
<template>
  <Suspense>
    <template #default>
      <AsyncComponent/>
    </template>
    <template #fallback>
      <loading-component/>
    </template>
  </Suspense>
</template>

<script>
import { defineAsyncComponent, Suspense } from 'vue';
import AsyncComponent from './AsyncComponent.vue';
import LoadingComponent from './LoadingComponent.vue';

export default {
  name: 'App',
  components: {
    AsyncComponent,
    LoadingComponent
  }
}
</script>
Copy after login

Conclusion:
The Suspense component in Vue 3 allows us to more easily implement data loading transition effects. By introducing the Suspense component, defining asynchronous components and custom loading status components, we can easily achieve the transition effect of data loading and improve user experience. I hope this article is helpful to you, thank you for reading!

The above is the detailed content of How to use the Suspense component in Vue 3 to achieve data loading transition effects. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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