Bird
Raised Fist0
Vueframework~20 mins

Async components for lazy loading in Vue - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Async Vue Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
What will this Vue async component render initially?

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?

ANothing is rendered until the component finishes loading after 2 seconds.
BThe text 'Loading...' is shown automatically during the 2 seconds delay.
CAn empty space is rendered because no loading or error component is provided.
DThe component throws an error because the async component is not awaited.
Attempts:
2 left
💡 Hint

Think about what Vue shows by default if no loading or error component is specified.

📝 Syntax
intermediate
2:00remaining
Which option correctly defines a Vue async component with a loading message?

You want to create an async component that shows a loading message while fetching. Which code snippet correctly implements this?

A
const AsyncComp = defineAsyncComponent(() => import('./MyComp.vue'), {
  loadingComponent: { template: '<p>Loading...</p>' }
});
B
const AsyncComp = defineAsyncComponent({
  loader: () => import('./MyComp.vue'),
  loadingComponent: { template: '<p>Loading...</p>' }
});
C
const AsyncComp = defineAsyncComponent({
  loader: () => import('./MyComp.vue'),
  loading: { template: '<p>Loading...</p>' }
});
D
const AsyncComp = defineAsyncComponent(() => import('./MyComp.vue'), {
  loading: { template: '<p>Loading...</p>' }
});
Attempts:
2 left
💡 Hint

Check the property names expected by defineAsyncComponent when using an options object.

🔧 Debug
advanced
2:00remaining
Why does this async component cause a runtime error?

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?

ABecause the import returns a module object, but the component is exported as a named export, not default.
BBecause the import promise resolves to the component itself, not an object with a default property.
CBecause the arrow function lacks braces and does not return the promise correctly.
DBecause the async function returns the component instance instead of the component definition.
Attempts:
2 left
💡 Hint

Check how the component is exported in MyComp.vue.

❓ state_output
advanced
2:00remaining
What is the rendered output after 3 seconds?

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?

AThe text 'Loading...' because the loading component stays visible indefinitely.
BNothing is shown because the delay hides the loading component.
CAn error message because the async component failed to load.
DThe text 'Async Loaded' because the async component finished loading after 2 seconds.
Attempts:
2 left
💡 Hint

Consider the delay and loading time carefully.

🧠 Conceptual
expert
2:00remaining
Which statement about Vue async components is TRUE?

Choose the correct statement about Vue 3 async components and lazy loading.

AYou can specify error and timeout components to handle loading failures gracefully.
BAsync components always block rendering until fully loaded, causing UI freeze.
CAsync components cannot be used with suspense in Vue 3.
DLazy loading async components requires manual import and cannot use dynamic import syntax.
Attempts:
2 left
💡 Hint

Think about Vue 3 features for async components and error handling.

Practice

(1/5)
1. What is the main purpose of using defineAsyncComponent in Vue?
easy
A. To load a component only when it is needed, improving app performance
B. To define a component that updates automatically on data change
C. To create a component that runs only on the server side
D. To make a component that never re-renders after first load

Solution

  1. Step 1: Understand what lazy loading means

    Lazy loading means loading something only when it is needed, not before.
  2. Step 2: Connect lazy loading to defineAsyncComponent

    This function helps Vue load components only when they are required, saving resources.
  3. Final Answer:

    To load a component only when it is needed, improving app performance -> Option A
  4. Quick Check:

    Lazy loading = load on demand [OK]
Hint: Async components load only when needed, boosting speed [OK]
Common Mistakes:
  • Thinking it updates components automatically
  • Confusing with server-side rendering
  • Believing it prevents re-renders forever
2. Which of the following is the correct syntax to create an async component in Vue 3?
easy
A. const AsyncComp = async () => import('./MyComp.vue')
B. const AsyncComp = defineAsyncComponent(() => import('./MyComp.vue'))
C. const AsyncComp = defineComponent(async () => import('./MyComp.vue'))
D. const AsyncComp = createAsyncComponent('./MyComp.vue')

Solution

  1. Step 1: Recall the Vue 3 async component syntax

    Vue 3 uses defineAsyncComponent with a function returning an import promise.
  2. Step 2: Match the syntax with options

    const AsyncComp = defineAsyncComponent(() => import('./MyComp.vue')) matches the correct pattern: defineAsyncComponent(() => import('...')).
  3. Final Answer:

    const AsyncComp = defineAsyncComponent(() => import('./MyComp.vue')) -> Option B
  4. Quick Check:

    Correct async syntax = defineAsyncComponent + import [OK]
Hint: Use defineAsyncComponent with arrow function returning import [OK]
Common Mistakes:
  • Using async arrow function without defineAsyncComponent
  • Using defineComponent instead of defineAsyncComponent
  • Calling a non-existent createAsyncComponent function
3. Given this Vue code snippet, what will be rendered initially?
const AsyncComp = defineAsyncComponent(() => new Promise(resolve => {
  setTimeout(() => resolve({ template: '
Loaded!
' }), 2000) })) export default { components: { AsyncComp }, template: `
Loading...
` }
medium
A. Loading... (fallback content) for 2 seconds, then Loaded!
B. Loaded! immediately without delay
C. Nothing renders until the component loads
D. An error because Suspense cannot be used with async components

Solution

  1. Step 1: Understand Suspense fallback behavior

    Suspense shows fallback content while async component loads.
  2. Step 2: Analyze the async component delay

    The component resolves after 2 seconds, so fallback shows first.
  3. Final Answer:

    Loading... (fallback content) for 2 seconds, then Loaded! -> Option A
  4. Quick Check:

    Suspense fallback shows during async load [OK]
Hint: Suspense fallback shows until async component resolves [OK]
Common Mistakes:
  • Expecting immediate component render
  • Thinking nothing shows during loading
  • Believing Suspense cannot wrap async components
4. Identify the error in this async component usage:
import { defineAsyncComponent } from 'vue'

const AsyncComp = defineAsyncComponent(() => import('./MyComp.vue'))

export default {
  components: { AsyncComp },
  template: `
` }
medium
A. Async component must be registered globally, not locally
B. The template uses instead of self-closing
C. Missing Suspense wrapper to handle loading state
D. The import path './MyComp.vue' is invalid syntax

Solution

  1. Step 1: Check async component usage rules

    Async components should be wrapped in <Suspense> to show fallback during loading.
  2. Step 2: Analyze the given code

    The code lacks a <Suspense> wrapper, so no loading state is handled.
  3. Final Answer:

    Missing Suspense wrapper to handle loading state -> Option C
  4. Quick Check:

    Async components need Suspense for loading fallback [OK]
Hint: Always wrap async components in Suspense for loading UI [OK]
Common Mistakes:
  • Thinking self-closing tags are mandatory
  • Believing async components must be global
  • Assuming import path syntax is wrong
5. You want to lazy load two components, CompA and CompB, but only show CompB after CompA finishes loading. How can you achieve this using Vue async components and Suspense?
hard
A. Wrap CompA in Suspense, and inside its default slot, include CompB as a normal component
B. Use defineAsyncComponent for both, wrap both in a single Suspense, and use v-if to show CompB only after CompA loads
C. Load both components with defineAsyncComponent and render them side by side without Suspense
D. Wrap CompA in a Suspense with fallback, then in CompA's mounted hook, render CompB inside another Suspense

Solution

  1. Step 1: Understand the loading order requirement

    You want CompB to appear only after CompA finishes loading.
  2. Step 2: Use sequential triggering with lifecycle

    Wrap CompA in Suspense; in CompA's mounted hook, dynamically render CompB (async) inside its own Suspense for true sequential loading.
  3. Step 3: Why other options fail

    Wrapping both in a single Suspense loads them in parallel (shows both after slowest finishes); loading side-by-side without Suspense skips fallback and loads parallel; including normal CompB in CompA's default slot loads it immediately upon CompA resolution.
  4. Final Answer:

    Wrap CompA in a Suspense with fallback, then in CompA's mounted hook, render CompB inside another Suspense -> Option D
  5. Quick Check:

    Sequential async = trigger next in mounted [OK]
Hint: Trigger next async component in mounted hook for sequential loading [OK]
Common Mistakes:
  • Using single Suspense for parallel loading
  • Skipping Suspense for async components
  • Treating second component as normal (non-async)
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard