📖 Scenario: You are building a simple Angular app that has multiple pages. To navigate between pages, you need to set up routes.
🎯 Goal: Create a routes array that defines paths and their corresponding components for navigation in an Angular app.
📋 What You'll Learn
Create a routes array with exactly three routes
Each route must have a path and a component
Use the exact paths: '', 'about', and 'contact'
Use the exact component names: HomeComponent, AboutComponent, and ContactComponent
💡 Why This Matters
🌍 Real World
Routing is essential in Angular apps to navigate between different pages or views without reloading the page.
💼 Career
Understanding how to define and export routes is a fundamental skill for Angular developers working on single-page applications.
Progress0 / 4 steps
1
Create routes array with home route
Create a constant called routes and assign it an array with one route object. The route object should have path set to an empty string '' and component set to HomeComponent.
Angular
Hint
Remember to use square brackets [] for the array and curly braces {} for the route object.
2
Add about route to routes array
Add a second route object to the routes array. This route should have path set to 'about' and component set to AboutComponent.
Angular
Hint
Separate route objects with commas inside the array.
3
Add contact route to routes array
Add a third route object to the routes array. This route should have path set to 'contact' and component set to ContactComponent.
Angular
Hint
Make sure to add a comma after the second route object before adding the third.
4
Export routes array
Add an export statement to export the routes array so it can be used in the Angular module.
Angular
Hint
Use export const to make the routes array available outside this file.
Practice
(1/5)
1. What does the routes array in Angular define?
easy
A. It defines the styles for each component.
B. It lists all services used in the app.
C. It tells Angular which component to show for each URL path.
D. It sets the database connection strings.
Solution
Step 1: Understand the purpose of routes array
The routes array maps URL paths to components so Angular knows what to display.
Step 2: Eliminate unrelated options
Styles, services, and database connections are not handled by routes.
Final Answer:
It tells Angular which component to show for each URL path. -> Option C
Quick Check:
Routes array = component mapping [OK]
Hint: Routes array links URL paths to components [OK]
Common Mistakes:
Confusing routes with styles or services
Thinking routes handle backend connections
Assuming routes define component logic
2. Which of the following is the correct way to define a route for path 'home' with component HomeComponent?
easy
A. { path: 'home', component: HomeComponent }
B. { path: 'home', redirectTo: HomeComponent }
C. { route: 'home', component: HomeComponent }
D. { path: 'home', component: 'HomeComponent' }
Solution
Step 1: Check correct property names
The route object must have path and component properties.
Step 2: Verify component value type
The component value should be the component class, not a string or redirect.
Final Answer:
{ path: 'home', component: HomeComponent } -> Option A
Quick Check:
Use path and component keys correctly [OK]
Hint: Use component class, not string or redirectTo for components [OK]
Common Mistakes:
Using 'route' instead of 'path'
Setting component as a string
Using redirectTo instead of component for components
A. redirectTo should be used with pathMatch property.
B. redirectTo cannot be used in routes array.
C. component is missing for 'profile' route.
D. Path 'user' should be capitalized.
Solution
Step 1: Check redirectTo usage
When using redirectTo, Angular requires a pathMatch property to specify matching strategy.
Step 2: Confirm missing pathMatch causes error
Without pathMatch, Angular cannot decide when to redirect, causing routing errors.
Final Answer:
redirectTo should be used with pathMatch property. -> Option A
Quick Check:
redirectTo needs pathMatch [OK]
Hint: Always add pathMatch with redirectTo routes [OK]
Common Mistakes:
Omitting pathMatch with redirectTo
Thinking redirectTo needs component
Assuming path names must be capitalized
5. You want to define routes so that: - The home page shows HomeComponent at path ''. - The about page shows AboutComponent at path 'about'. - Any unknown path redirects to home.