What is the purpose of defining routes in a Vue application?
Defining routes lets you connect URLs to specific components. It helps users navigate different pages or views in the app without reloading the whole page.
Click to reveal answer
beginner
How do you define a simple route in Vue Router?
You create a route object with a path and a component. For example:
{ path: '/home', component: Home }
This means when the URL is '/home', the Home component shows.
Click to reveal answer
intermediate
What is the role of createRouter and createWebHistory in Vue Router?
createRouter makes the router instance with all routes. createWebHistory tells Vue Router to use browser history mode, so URLs look clean without hashes (#).
Click to reveal answer
intermediate
How can you define nested routes in Vue Router?
Nested routes are defined inside a route's children array. Each child route has its own path and component. This lets you show components inside parent components.
Click to reveal answer
intermediate
What happens if a user visits a URL that does not match any defined route?
You can define a 'catch-all' route with path /:pathMatch(.*)* to show a 'Not Found' component. This handles unknown URLs gracefully.
Click to reveal answer
Which Vue Router function creates the router instance?
AcreateRoute
BdefineRouter
CuseRouter
DcreateRouter
✗ Incorrect
createRouter is used to create the router instance with routes and history mode.
What does the path property in a route define?
AThe component to render
BThe URL segment to match
CThe router mode
DThe navigation guard
✗ Incorrect
The path defines the URL segment that triggers the route.
How do you define a nested route in Vue Router?
AInside the <code>children</code> array of a route
BUsing <code>nested: true</code> property
CBy adding multiple <code>path</code> properties
DBy creating separate router instances
✗ Incorrect
Nested routes go inside the children array of a parent route.
Which history mode removes the hash (#) from URLs in Vue Router?
AcreateWebHashHistory
BcreateMemoryHistory
CcreateWebHistory
DcreateHistory
✗ Incorrect
createWebHistory uses the browser's history API for clean URLs without hashes.
What path pattern is used to catch all unmatched routes in Vue Router?
A/:pathMatch(.*)*
B/404
C/notfound
D/:catchAll*
✗ Incorrect
The pattern /:pathMatch(.*)* matches any route not defined earlier.
Explain how to set up basic routing in a Vue application using Vue Router.
Think about how URLs connect to components and how Vue Router manages this.
You got /5 concepts.
Describe how nested routes work in Vue Router and why they are useful.
Imagine a page with tabs or sections inside it.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of defining routes in a Vue application?
easy
A. To style components with CSS
B. To connect URLs to components so different pages can be shown
C. To manage state between components
D. To handle user input events
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 B
Quick 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
A. { path: '/home', component: HomeView }
B. { route: '/home', view: HomeView }
C. { url: '/home', component: HomeView }
D. { path: '/home', view: HomeView }
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 A
Quick Check:
Route keys = path + component [OK]
Hint: Use 'path' and 'component' keys to define routes [OK]
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 C
Quick 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?