Lazy loading routes means loading parts of your app only when needed. This helps your app start faster and saves data.
Lazy loading routes in Vue
const routes = [ { path: '/home', component: () => import('./components/Home.vue') }, { path: '/about', component: () => import('./components/About.vue') } ];
Use a function that returns import() to load the component only when the route is visited.
This syntax works with Vue Router and modern JavaScript modules.
const routes = [ { path: '/dashboard', component: () => import('./Dashboard.vue') } ];
const routes = [ { path: '/profile', component: () => import(/* webpackChunkName: "profile" */ './Profile.vue') } ];
This Vue Router setup lazy loads the Home and About components. They load only when you visit their routes, making the app start faster.
import { createRouter, createWebHistory } from 'vue-router'; const routes = [ { path: '/', component: () => import('./components/Home.vue') }, { path: '/about', component: () => import('./components/About.vue') } ]; const router = createRouter({ history: createWebHistory(), routes }); export default router;
Lazy loading improves app speed but adds a small delay when loading a new route for the first time.
Use lazy loading for large or rarely used components to save bandwidth.
Check browser DevTools Network tab to see when chunks load.
Lazy loading routes loads components only when needed.
This reduces initial app size and speeds up start time.
Use component: () => import('...') syntax in Vue Router.
Practice
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 CQuick Check:
Lazy loading = load on demand [OK]
- Thinking lazy loading preloads all components
- Confusing lazy loading with caching
- Believing lazy loading disables routing
Solution
Step 1: Recall Vue Router lazy loading syntax
Vue Router uses dynamic import with arrow function: () => import('path').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.Final Answer:
component: () => import('./MyComponent.vue') -> Option DQuick Check:
Lazy load syntax = arrow function + import() [OK]
- Omitting arrow function
- Using require() instead of import()
- Assigning component to string path directly
const routes = [
{
path: '/dashboard',
component: () => import('./Dashboard.vue')
}
]Solution
Step 1: Analyze the route config
The component is assigned as a function returning import(), which means lazy loading.Step 2: Understand lazy loading behavior
The component will load only when the route '/dashboard' is visited, not before.Final Answer:
The Dashboard component is loaded only when the '/dashboard' route is visited. -> Option BQuick Check:
Lazy load means load on visit [OK]
- Assuming component loads at app start
- Thinking component never loads
- Believing component loads multiple times automatically
const routes = [
{
path: '/profile',
component: import('./Profile.vue')
}
]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 AQuick Check:
Lazy load needs () => import() [OK]
- Calling import() directly without arrow function
- Changing path unnecessarily
- Believing lazy loading is unsupported
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 AQuick Check:
Lazy load each nested route with () => import() [OK]
- Lazy loading only parent route
- Using static imports for lazy loading
- Separating child routes incorrectly
Ready to prove it?
Pass the test and this pattern moves unknown
