Routes help your app show different pages when users click links or enter URLs. They make your app feel like a website with many pages.
Defining routes in Vue
Introduction
Syntax
Vue
import { createRouter, createWebHistory } from 'vue-router' const routes = [ { path: '/', component: Home }, { path: '/about', component: About } ] const router = createRouter({ history: createWebHistory(), routes }) export default router
The routes array lists all pages with their URL paths and components.
createRouter sets up the router with history mode and routes.
Examples
Vue
const routes = [ { path: '/', component: Home }, { path: '/contact', component: Contact } ]
:id to show different user profiles.Vue
const routes = [ { path: '/user/:id', component: UserProfile } ]
Vue
const routes = [ { path: '/:pathMatch(.*)*', component: NotFound } ]
Sample Program
This Vue app shows a home page at '/' and an about page at '/about'. The router changes the displayed page without reloading.
Vue
import { createApp } from 'vue' import { createRouter, createWebHistory } from 'vue-router' const Home = { template: '<h1>Home Page</h1>' } const About = { template: '<h1>About Page</h1>' } const routes = [ { path: '/', component: Home }, { path: '/about', component: About } ] const router = createRouter({ history: createWebHistory(), routes }) const app = createApp({}) app.use(router) app.mount('#app')
Important Notes
Always import and use the router in your main Vue app to enable routing.
Use router-link components to create accessible links between routes.
Test routes in the browser by typing URLs or clicking links to see pages change.
Summary
Routes connect URLs to components to show different pages.
Define routes in an array with path and component.
Use createRouter and createWebHistory to set up routing.
Practice
1. What is the main purpose of defining routes in a Vue application?
easy
Solution
Step 1: Understand the role of routes
Routes link specific URLs to Vue components, allowing navigation between pages.Step 2: Identify the correct purpose
Connecting URLs to components matches the description of routing.Final Answer:
To connect URLs to components so different pages can be shown -> Option BQuick Check:
Routes = URL to component mapping [OK]
Hint: Routes map URLs to components for page navigation [OK]
Common Mistakes:
- Confusing routes with styling or CSS
- Thinking routes manage state or events
- Mixing routes with user input handling
2. Which of the following is the correct way to define a route for path '/home' using a component named HomeView?
easy
Solution
Step 1: Recall route object keys
Routes use keys 'path' for URL and 'component' for the Vue component.Step 2: Match correct syntax
{ path: '/home', component: HomeView } uses correct keys: path and component with proper values.Final Answer:
{ path: '/home', component: HomeView } -> Option AQuick Check:
Route keys = path + component [OK]
Hint: Use 'path' and 'component' keys to define routes [OK]
Common Mistakes:
- Using 'route' or 'url' instead of 'path'
- Using 'view' instead of 'component'
- Mixing keys or misspelling them
3. Given this route setup:
const routes = [
{ path: '/', component: HomeView },
{ path: '/about', component: AboutView }
];
const router = createRouter({
history: createWebHistory(),
routes
});
What component will show when the URL is '/about'?medium
Solution
Step 1: Check the route matching '/about'
The route with path '/about' uses component AboutView.Step 2: Understand router behavior
When URL is '/about', Vue Router renders AboutView component.Final Answer:
AboutView -> Option AQuick Check:
URL '/about' shows AboutView [OK]
Hint: URL matches route path to show its component [OK]
Common Mistakes:
- Confusing router functions with components
- Choosing HomeView for '/about' URL
- Not linking URL to correct route
4. Identify the error in this route definition:
const routes = [
{ path: '/contact', component ContactView }
];medium
Solution
Step 1: Check object syntax in route
Each key-value pair must have a colon ':' separating them.Step 2: Spot missing colon after 'component'
In the route, 'component ContactView' misses ':'; it should be 'component: ContactView'.Final Answer:
Missing colon ':' after 'component' -> Option CQuick Check:
Object syntax needs ':' [OK]
Hint: Check colons between keys and values in objects [OK]
Common Mistakes:
- Forgetting colon ':' in object properties
- Using '#' instead of '/' in paths
- Putting component names as strings incorrectly
5. You want to create a Vue router with two routes: '/' showing HomeView and '/profile/:id' showing ProfileView. Which code correctly defines these routes and sets up the router?
hard
Solution
Step 1: Define routes with dynamic segment
Use '/profile/:id' to capture dynamic id parameter correctly.Step 2: Use correct component references and history mode
Components must be variables (not strings). Use createWebHistory() for normal web routing.Step 3: Check each option
const routes = [ { path: '/', component: HomeView }, { path: '/profile/:id', component: ProfileView } ]; const router = createRouter({ history: createWebHistory(), routes }); matches all requirements: dynamic path, component variables, and web history.Final Answer:
Correct route array with dynamic segment and createWebHistory() -> Option DQuick Check:
Dynamic routes use ':param' and createWebHistory() [OK]
Hint: Use ':param' for dynamic routes and createWebHistory() for browser routing [OK]
Common Mistakes:
- Using literal 'id' instead of ':id' for dynamic segments
- Passing component names as strings instead of variables
- Using createMemoryHistory() instead of createWebHistory() for web apps
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
