Consider this Vue 3 async component setup using defineAsyncComponent:
import { defineAsyncComponent } from 'vue';
const AsyncComp = defineAsyncComponent(() => new Promise(resolve => {
setTimeout(() => {
resolve({ template: 'Loaded!' });
}, 2000);
}));
export default {
components: { AsyncComp },
template: ' '
}What will the user see immediately after the component starts loading?
Think about what Vue shows by default if no loading or error component is specified.
When using defineAsyncComponent without specifying a loading component, Vue renders nothing (empty space) while waiting for the async component to load.
You want to create an async component that shows a loading message while fetching. Which code snippet correctly implements this?
Check the property names expected by defineAsyncComponent when using an options object.
The correct property for the loading component is loadingComponent. It must be used inside an options object with a loader function.
Look at this Vue async component definition:
const AsyncComp = defineAsyncComponent(() => {
return import('./MyComp.vue').then(module => module.default);
});When used, it throws: TypeError: module.default is not a constructor. Why?
Check how the component is exported in MyComp.vue.
If the component is exported as a named export, accessing module.default is undefined, causing the error. You must import the named export correctly.
Given this Vue 3 component using async loading:
import { defineAsyncComponent, ref } from 'vue';
const AsyncComp = defineAsyncComponent({
loader: () => new Promise(resolve => {
setTimeout(() => resolve({ template: 'Async Loaded' }), 2000);
}),
loadingComponent: { template: 'Loading...' },
delay: 500
});
export default {
components: { AsyncComp },
template: ' '
};What will the user see after 3 seconds?
Consider the delay and loading time carefully.
The async component loads after 2 seconds. The loading component shows after 0.5 seconds delay. After 3 seconds, the async component is fully loaded and rendered.
Choose the correct statement about Vue 3 async components and lazy loading.
Think about Vue 3 features for async components and error handling.
Vue 3 allows specifying error and timeout components in async component options to show UI feedback if loading fails or takes too long.