Consider this Vue 3 component using <Suspense> with an async child component. What will the user see first when this component is mounted?
<template>
<Suspense>
<template #default>
<AsyncChild />
</template>
<template #fallback>
<p>Loading...</p>
</template>
</Suspense>
</template>
<script setup>
import { defineAsyncComponent } from 'vue'
const AsyncChild = defineAsyncComponent(() =>
new Promise(resolve => {
setTimeout(() => {
resolve({ template: '<p>Async content loaded</p>' })
}, 2000)
})
)
</script>Think about what the fallback slot in <Suspense> is for.
The <Suspense> component shows the fallback content immediately while waiting for the async component to load. Once loaded, it replaces the fallback with the async component's content.
isPending during async component loading?In Vue 3, when using defineAsyncComponent with Suspense, you can track loading state with isPending. What is the value of isPending while the async component is still loading?
<script setup> import { defineAsyncComponent, ref } from 'vue' const isPending = ref(false) const AsyncComp = defineAsyncComponent({ loader: () => new Promise(resolve => setTimeout(() => resolve({ template: '<p>Done</p>' }), 3000)), onPending() { isPending.value = true }, onResolve() { isPending.value = false } }) </script>
Check what onPending does in defineAsyncComponent.
The onPending hook sets isPending to true when the async component starts loading. It stays true until the component resolves.
Given two async components AsyncA and AsyncB, which code snippet correctly uses <Suspense> to show a fallback while both load?
Remember the correct slot names for Suspense fallback and default content.
The <Suspense> component requires the fallback content inside a named slot #fallback and the async components inside the default slot. Option A correctly uses both slots.
Look at this Vue 3 code. The fallback slot content never appears even though the async component takes 3 seconds to load. Why?
<template>
<Suspense>
<AsyncComponent />
<template #fallback>
<p>Loading...</p>
</template>
</Suspense>
</template>
<script setup>
import { defineAsyncComponent } from 'vue'
const AsyncComponent = defineAsyncComponent(() =>
new Promise(resolve => setTimeout(() => resolve({ template: '<p>Loaded</p>' }), 3000))
)
</script>Check how slots are structured inside Suspense.
Suspense expects async components inside the default slot. Here, <AsyncComponent /> is a direct child, not inside a <template #default>. So fallback is ignored and async component renders immediately when ready.
When a Vue Suspense component wraps multiple async components, how does it decide when to switch from fallback to default content?
Think about the user experience when multiple async components load.
Vue Suspense waits for all async components inside the default slot to finish loading before replacing the fallback with the full default content. This ensures a smooth transition without partial content flashes.