Lazy loading components helps your app load faster by only loading parts when needed. It saves time and data by not loading everything at once.
Lazy loading components in Vue
Introduction
Syntax
Vue
import { defineAsyncComponent } from 'vue' const MyComponent = defineAsyncComponent(() => import('./MyComponent.vue'))
Use
defineAsyncComponent from Vue to create a lazy loaded component.The component is loaded only when it is actually needed in the app.
Examples
LazyComp.Vue
import { defineAsyncComponent } from 'vue' const LazyComp = defineAsyncComponent(() => import('./LazyComp.vue'))
defineAsyncComponent.Vue
<template> <LazyComp /> </template> <script setup> import { defineAsyncComponent } from 'vue' const LazyComp = defineAsyncComponent(() => import('./LazyComp.vue')) </script>
Vue
const LazyComp = defineAsyncComponent({ loader: () => import('./LazyComp.vue'), loadingComponent: LoadingSpinner, delay: 200 })
Sample Program
This Vue component shows a button to toggle a lazy loaded component. The component loads only when you click the button. While loading, it shows a fallback message.
Vue
<template> <button @click="show = !show">Toggle Lazy Component</button> <Suspense> <template #default> <LazyComponent v-if="show" /> </template> <template #fallback> <p>Loading component...</p> </template> </Suspense> </template> <script setup> import { ref, defineAsyncComponent } from 'vue' const show = ref(false) const LazyComponent = defineAsyncComponent(() => import('./LazyComponent.vue')) </script>
Important Notes
Lazy loading helps reduce initial load time but may cause a small delay when the component first appears.
Use <Suspense> in Vue to show fallback content while loading.
Lazy loading is great for big apps or rarely used parts.
Summary
Lazy loading loads components only when needed to speed up app start.
Use defineAsyncComponent to create lazy loaded components in Vue.
Combine with <Suspense> to show loading states nicely.
Practice
1. What is the main benefit of lazy loading components in Vue?
easy
Solution
Step 1: Understand lazy loading purpose
Lazy loading means loading parts of the app only when needed, not all at once.Step 2: Identify benefit in Vue context
Loading components only when needed reduces initial load time, making app start faster.Final Answer:
It speeds up the initial loading time by loading components only when needed. -> Option CQuick Check:
Lazy loading = faster start [OK]
Hint: Lazy loading delays loading until needed to speed start [OK]
Common Mistakes:
- Thinking lazy loading loads all components at once
- Confusing lazy loading with bug fixing
- Assuming lazy loading disables components
2. Which is the correct way to define a lazy loaded component in Vue using
defineAsyncComponent?easy
Solution
Step 1: Recall syntax for lazy loading
Vue uses defineAsyncComponent with a function returning import() to lazy load.Step 2: Match correct syntax
const MyComp = defineAsyncComponent(() => import('./MyComp.vue')) correctly wraps import in a function passed to defineAsyncComponent.Final Answer:
const MyComp = defineAsyncComponent(() => import('./MyComp.vue')) -> Option DQuick Check:
defineAsyncComponent + () => import() = correct [OK]
Hint: Use defineAsyncComponent with arrow function returning import [OK]
Common Mistakes:
- Using import directly without wrapping in a function
- Confusing defineComponent with defineAsyncComponent
- Using async keyword incorrectly with import
3. Given this Vue code snippet, what will be rendered initially?
const AsyncComp = defineAsyncComponent(() => import('./AsyncComp.vue'))
export default {
components: { AsyncComp },
template: ``
}medium
Solution
Step 1: Understand Suspense behavior
Suspense shows fallback content only if #fallback slot is provided; otherwise nothing renders while waiting for async components.Step 2: Analyze initial render
The code provides only default slot, no fallback slot, so nothing renders until AsyncComp resolves.Final Answer:
Nothing renders until AsyncComp finishes loading. -> Option AQuick Check:
Suspense no fallback = nothing renders [OK]
Hint: Suspense without fallback renders nothing during lazy load [OK]
Common Mistakes:
- Assuming Suspense shows loading fallback without #fallback slot
- Assuming lazy component renders instantly
- Thinking Suspense is unsupported in Vue 3
4. What is wrong with this lazy loading code snippet?
const AsyncComp = defineAsyncComponent(import('./AsyncComp.vue'))medium
Solution
Step 1: Check defineAsyncComponent usage
It requires a function returning import(), not the import promise directly.Step 2: Identify error cause
Passing import() directly triggers immediate loading, breaking lazy loading.Final Answer:
The import call is not wrapped in a function, causing immediate loading. -> Option AQuick Check:
Wrap import in function for lazy loading [OK]
Hint: Always wrap import in a function for defineAsyncComponent [OK]
Common Mistakes:
- Passing import() directly without function
- Thinking import path must be absolute
- Believing async components must be global
5. You want to lazy load two components,
CompA and CompB, but only show CompB after CompA finishes loading and rendering. Which approach correctly achieves this?hard
Solution
Step 1: Understand sequential lazy loading need
We wantCompBto appear only afterCompAfully loads and renders.Step 2: Use Suspense and conditional rendering
WrapCompAin Suspense to handle loading, then showCompBconditionally afterCompAis ready.Final Answer:
Use two Suspense components, render CompA inside first Suspense, then conditionally render CompB after CompA loads. -> Option BQuick Check:
Sequential lazy load needs Suspense + conditional render [OK]
Hint: Use Suspense + conditional render for sequential lazy components [OK]
Common Mistakes:
- Rendering both lazy components together without order control
- Not using Suspense for loading states
- Trying to lazy load inside setup without Suspense
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
