suspense component in vuejs

 Sure, here's an example of how to use the Suspense component in Vue 3 to handle asynchronous components:

First, let's create a component that will be loaded asynchronously. We'll call it AsyncComponent.vue

<template>
<div>
<h2>Hello from AsyncComponent!</h2>
</div>
</template>

<script>
export default {
name: 'AsyncComponent',
async setup() {
// Simulate async operation
await new Promise(resolve => setTimeout(resolve, 2000));
}
}
</script>

Next, let's create the parent component that will use the Suspense component to handle the asynchronous loading of AsyncComponent.vue. We'll call it ParentComponent.vue:

<template>
<div>
<h1>Hello from ParentComponent!</h1>
<Suspense>
<template #default>
<AsyncComponent />
</template>
<template #fallback>
<div>Loading...</div>
</template>
</Suspense>
</div>
</template>

<script>
import AsyncComponent from './AsyncComponent.vue';

export default {
name: 'ParentComponent',
components: {
AsyncComponent
}
}
</script>

In this example, the Suspense component is used to wrap the AsyncComponent. The #default slot is used to display the AsyncComponent when it's loaded, and the #fallback slot is used to display a loading message while the AsyncComponent is being loaded.

When you run this code, you should see the "Loading..." message for 2 seconds, and then the "Hello from AsyncComponent!" message will appear.

=

No comments:

Post a Comment

Event listening in react

 How we can listen to som eevents some envents fire like click or automatically user enters into input button , that is event on word type i...