Bird
Raised Fist0
Vueframework~10 mins

Lazy loading routes in Vue - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Lazy loading routes
User navigates to route
↓
Check if route component is loaded
↓
Load component asynchronously
↓
Render component
↓
User interacts with page
When a user goes to a route, Vue checks if the component is loaded. If not, it loads it asynchronously, then renders it.
Execution Sample
Vue
import { createRouter, createWebHistory } from 'vue-router';

const routes = [
  { path: '/home', component: () => import('./Home.vue') }
];

const router = createRouter({ history: createWebHistory(), routes });

export default router;
Defines a route that loads the Home component only when the user visits '/home'.
Execution Table
StepActionRoute PathComponent Loaded?Result
1User navigates to '/'/NoNo component loaded, default page shown
2User clicks link to '/home'/homeNoStart loading Home.vue asynchronously
3Home.vue finishes loading/homeYesRender Home component
4User navigates again to '/home'/homeYesRender Home component immediately without loading
5User navigates to unknown route/unknownNoShow 404 or fallback page
💡 Execution stops when the component is loaded and rendered or route is unknown.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
currentRoute//home/home/home/home
componentLoadedfalseloadingtruetruetrue
Key Moments - 3 Insights
Why does the component load only when I visit the route?
Because the component is wrapped in a function that imports it asynchronously, as shown in Step 2 and 3 of the execution table.
What happens if I visit the same route again?
The component is already loaded (Step 4), so Vue renders it immediately without loading again.
What if I visit a route that is not defined?
Vue shows a fallback or 404 page as in Step 5, because no component is loaded for unknown routes.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of 'componentLoaded' after Step 3?
A"true"
B"false"
C"loading"
D"undefined"
💡 Hint
Check the variable_tracker table column 'After Step 3' for 'componentLoaded'.
At which step does Vue start loading the component asynchronously?
AStep 1
BStep 2
CStep 4
DStep 5
💡 Hint
Look at the 'Action' and 'Result' columns in the execution_table for when loading starts.
If the user visits '/home' twice, what changes in the component loading process?
AThe component never loads.
BThe component loads twice asynchronously.
CThe component loads once and renders immediately the second time.
DThe component unloads after first visit.
💡 Hint
Refer to Steps 3 and 4 in the execution_table and variable_tracker for componentLoaded state.
Concept Snapshot
Lazy loading routes in Vue:
- Define routes with components as functions returning import()
- Component loads only when route is visited
- Improves app speed by loading code on demand
- Subsequent visits use cached component
- Unknown routes show fallback page
Full Transcript
Lazy loading routes in Vue means the app waits to load a route's component until the user actually visits that route. When the user clicks a link to a lazy route, Vue starts loading the component asynchronously. Once loaded, Vue renders the component. If the user visits the same route again, Vue uses the already loaded component and renders it immediately. If the user visits a route that does not exist, Vue shows a fallback or 404 page. This approach helps keep the app fast by not loading all components at once.

Practice

(1/5)
1. What is the main benefit of using lazy loading for routes in Vue?
easy
A. It automatically caches all components in the browser.
B. It preloads all components at app start for faster navigation.
C. It loads components only when the route is visited, reducing initial load time.
D. It disables routing and shows all components on one page.

Solution

  1. Step 1: Understand lazy loading concept

    Lazy loading means loading something only when it is needed, not before.
  2. Step 2: Apply to Vue routes

    In Vue, lazy loading routes means components load only when their route is visited, reducing initial app size and speeding start time.
  3. Final Answer:

    It loads components only when the route is visited, reducing initial load time. -> Option C
  4. Quick Check:

    Lazy loading = load on demand [OK]
Hint: Lazy loading means load components only when needed [OK]
Common Mistakes:
  • Thinking lazy loading preloads all components
  • Confusing lazy loading with caching
  • Believing lazy loading disables routing
2. Which of the following is the correct syntax to lazy load a route component in Vue Router?
easy
A. component: import('./MyComponent.vue')
B. component: './MyComponent.vue'
C. component: () => require('./MyComponent.vue')
D. component: () => import('./MyComponent.vue')

Solution

  1. Step 1: Recall Vue Router lazy loading syntax

    Vue Router uses dynamic import with arrow function: () => import('path').
  2. Step 2: Check each option

    component: () => import('./MyComponent.vue') matches correct syntax. component: import('./MyComponent.vue') misses arrow function. component: () => require('./MyComponent.vue') uses require which is not standard for lazy loading in Vue 3. component: './MyComponent.vue' is just a string, not a function.
  3. Final Answer:

    component: () => import('./MyComponent.vue') -> Option D
  4. Quick Check:

    Lazy load syntax = arrow function + import() [OK]
Hint: Lazy load uses arrow function returning import() [OK]
Common Mistakes:
  • Omitting arrow function
  • Using require() instead of import()
  • Assigning component to string path directly
3. Given this Vue Router route config, what will happen when the user visits '/dashboard' for the first time?
const routes = [
  {
    path: '/dashboard',
    component: () => import('./Dashboard.vue')
  }
]
medium
A. The Dashboard component is loaded immediately when the app starts.
B. The Dashboard component is loaded only when the '/dashboard' route is visited.
C. The Dashboard component is never loaded.
D. The Dashboard component is loaded twice.

Solution

  1. Step 1: Analyze the route config

    The component is assigned as a function returning import(), which means lazy loading.
  2. Step 2: Understand lazy loading behavior

    The component will load only when the route '/dashboard' is visited, not before.
  3. Final Answer:

    The Dashboard component is loaded only when the '/dashboard' route is visited. -> Option B
  4. Quick Check:

    Lazy load means load on visit [OK]
Hint: Lazy loaded components load on route visit, not app start [OK]
Common Mistakes:
  • Assuming component loads at app start
  • Thinking component never loads
  • Believing component loads multiple times automatically
4. Identify the error in this Vue Router lazy loading route config:
const routes = [
  {
    path: '/profile',
    component: import('./Profile.vue')
  }
]
medium
A. Missing arrow function wrapping the import call.
B. The path should be '/profiles' instead of '/profile'.
C. The import path must be absolute, not relative.
D. Lazy loading is not supported in Vue Router.

Solution

  1. Step 1: Check lazy loading syntax

    Lazy loading requires component to be a function returning import(), like () => import('...').
  2. Step 2: Identify the error

    Here, import() is called directly without arrow function, causing immediate import and syntax error.
  3. Final Answer:

    Missing arrow function wrapping the import call. -> Option A
  4. Quick Check:

    Lazy load needs () => import() [OK]
Hint: Lazy load must use arrow function wrapping import() [OK]
Common Mistakes:
  • Calling import() directly without arrow function
  • Changing path unnecessarily
  • Believing lazy loading is unsupported
5. You want to lazy load two nested routes in Vue Router: '/settings' and '/settings/profile'. Which is the correct way to configure lazy loading for both routes?
hard
A. Use component: () => import() for both routes inside children array.
B. Lazy load only the parent route; child routes load immediately.
C. Use static imports for both routes to avoid delays.
D. Define child routes outside the parent route config.

Solution

  1. Step 1: Understand nested route lazy loading

    Each route can lazy load its component independently using () => import().
  2. Step 2: Apply to nested routes

    Both '/settings' and '/settings/profile' should have component properties with arrow functions returning import() to lazy load separately.
  3. Step 3: Check other options

    Lazy load only the parent route; child routes load immediately. is wrong because child routes won't lazy load automatically. Use static imports for both routes to avoid delays. disables lazy loading. Define child routes outside the parent route config. is unrelated to lazy loading.
  4. Final Answer:

    Use component: () => import() for both routes inside children array. -> Option A
  5. Quick Check:

    Lazy load each nested route with () => import() [OK]
Hint: Lazy load each nested route with arrow function import() [OK]
Common Mistakes:
  • Lazy loading only parent route
  • Using static imports for lazy loading
  • Separating child routes incorrectly
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