📖 Scenario: You are building a Vue app with multiple pages. To make the app faster, you want to load each page only when the user visits it. This is called lazy loading routes.
🎯 Goal: Create a Vue router setup that lazy loads two routes: Home and About. Each route should load its component only when visited.
📋 What You'll Learn
Create a routes array with two routes: / for Home and /about for About
Use dynamic import syntax to lazy load the Home and About components
Create a Vue router instance using createRouter and createWebHistory
Export the router instance as default
💡 Why This Matters
🌍 Real World
Lazy loading routes helps make Vue apps faster by loading pages only when needed, improving user experience especially on slow networks.
💼 Career
Understanding lazy loading routes is important for frontend developers working with Vue to optimize app performance and scalability.
Progress0 / 4 steps
1
Set up the routes array
Create a constant called routes that is an array with two objects. The first object has path: '/' and name: 'Home'. The second object has path: '/about' and name: 'About'.
Vue
Hint
Think of routes as a list of pages. Each page has a path and a name.
2
Add lazy loading to route components
Update the routes array so that each route has a component property. Use dynamic import syntax: for Home, use () => import('./components/Home.vue'). For About, use () => import('./components/About.vue').
Vue
Hint
Use arrow functions with import() to load components only when needed.
3
Create the Vue router instance
Import createRouter and createWebHistory from 'vue-router'. Then create a constant called router by calling createRouter with an object that has history: createWebHistory() and routes.
Vue
Hint
Vue Router needs a history mode and the routes list to work.
4
Export the router instance
Add a line to export the router constant as the default export.
Vue
Hint
Exporting the router lets your Vue app use it.
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
Step 1: Understand lazy loading concept
Lazy loading means loading something only when it is needed, not before.
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.
Final Answer:
It loads components only when the route is visited, reducing initial load time. -> Option C
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?
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.
Final Answer:
component: () => import('./MyComponent.vue') -> Option D
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?
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
Step 1: Check lazy loading syntax
Lazy loading requires component to be a function returning import(), like () => import('...').
Step 2: Identify the error
Here, import() is called directly without arrow function, causing immediate import and syntax error.
Final Answer:
Missing arrow function wrapping the import call. -> Option A
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
Step 1: Understand nested route lazy loading
Each route can lazy load its component independently using () => import().
Step 2: Apply to nested routes
Both '/settings' and '/settings/profile' should have component properties with arrow functions returning import() to lazy load separately.
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.
Final Answer:
Use component: () => import() for both routes inside children array. -> Option A
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 unknownstrongwhile earning +1 Grip toward your certificate.