# Common Routing Tasks
This topic describes how to implement many of the common tasks associated with adding the Angular router to your application.
## Generate an application
The following command uses the Angular CLI to generate a basic Angular application with application routes.
The application name in the following example is `routing-app`.
ng new routing-app
### Adding components for routing
To use the Angular router, an application needs to have at least two components so that it can navigate from one to the other.
To create a component using the CLI, enter the following at the command line where `first` is the name of your component:
ng generate component first
Repeat this step for a second component but give it a different name.
Here, the new name is `second`.
ng generate component second
The CLI automatically appends `Component`, so if you were to write `first-component`, your component would be `FirstComponentComponent`.
<base href>
This guide works with a CLI-generated Angular application.
### Importing your new components
To use your new components, import them into `app.routes.ts` at the top of the file, as follows:
```
import {FirstComponent} from './first/first.component';
import {SecondComponent} from './second/second.component';
```
## Defining a basic route
There are three fundamental building blocks to creating a route.
Import the routes into `app.config.ts` and add it to the `provideRouter` function.
The Angular CLI performs this step for you.
However, if you are creating an application manually or working with an existing, non-CLI application, verify that the imports and configuration are correct.
The following is the default `ApplicationConfig` using the CLI.
```
export const appConfig: ApplicationConfig = {
providers: [provideRouter(routes)]
};
```
1. Set up a `Routes` array for your routes
The Angular CLI performs this step automatically.
```
import { Routes } from '@angular/router';
export const routes: Routes = [];
```
1. Define your routes in your `Routes` array.
Each route in this array is a JavaScript object that contains two properties.
The first property, `path`, defines the URL path for the route.
The second property, `component`, defines the component Angular should use for the corresponding path.
```
const routes: Routes = [
{ path: 'first-component', component: FirstComponent },
{ path: 'second-component', component: SecondComponent },
];
```
1. Add your routes to your application.
Now that you have defined your routes, add them to your application.
First, add links to the two components.
Assign the anchor tag that you want to add the route to the `routerLink` attribute.
Set the value of the attribute to the component to show when a user clicks on each link.
Next, update your component template to include ``.
This element informs Angular to update the application view with the component for the selected route.
```
Angular Router App
```
You also need to add the `RouterLink`, `RouterLinkActive`, and `RouterOutlet` to the imports array of `AppComponent`.
```
@Component({
selector: 'app-root',
standalone: true,
imports: [CommonModule, RouterOutlet, RouterLink, RouterLinkActive],
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'routing-app';
}
```
### Route order
The order of routes is important because the `Router` uses a first-match wins strategy when matching routes, so more specific routes should be placed above less specific routes.
List routes with a static path first, followed by an empty path route, which matches the default route.
The [wildcard route](guide/router#setting-up-wildcard-routes) comes last because it matches every URL and the `Router` selects it only if no other routes match first.
## Getting route information
Often, as a user navigates your application, you want to pass information from one component to another.
For example, consider an application that displays a shopping list of grocery items.
Each item in the list has a unique `id`.
To edit an item, users click an Edit button, which opens an `EditGroceryItem` component.
You want that component to retrieve the `id` for the grocery item so it can display the right information to the user.
Use a route to pass this type of information to your application components.
To do so, you use the [withComponentInputBinding](api/router/withComponentInputBinding) feature with `provideRouter` or the `bindToComponentInputs` option of `RouterModule.forRoot`.
To get information from a route:
1. Add the `withComponentInputBinding` feature to the `provideRouter` method.
1. Update the component to have an `Input` matching the name of the parameter.
**NOTE:**
You can bind all route data with key, value pairs to component inputs: static or resolved route data, path parameters, matrix parameters, and query parameters.
If you want to use the parent components route info you will need to set the router {@link paramsInheritanceStrategy} option:
`withRouterConfig({paramsInheritanceStrategy: 'always'})`
## Setting up wildcard routes
A well-functioning application should gracefully handle when users attempt to navigate to a part of your application that does not exist.
To add this functionality to your application, you set up a wildcard route.
The Angular router selects this route any time the requested URL doesn't match any router paths.
To set up a wildcard route, add the following code to your `routes` definition.
```
{ path: '**', component: PageNotFoundComponent }
```
The two asterisks, `**`, indicate to Angular that this `routes` definition is a wildcard route.
For the component property, you can define any component in your application.
Common choices include an application-specific `PageNotFoundComponent`, which you can define to [display a 404 page](guide/router#404-page-how-to) to your users; or a redirect to your application's main component.
A wildcard route is the last route because it matches any URL.
For more detail on why order matters for routes, see [Route order](guide/router#route-order).
## Displaying a 404 page
To display a 404 page, set up a [wildcard route](guide/router#wildcard-route-how-to) with the `component` property set to the component you'd like to use for your 404 page as follows:
```
const routes: Routes = [
{ path: 'first-component', component: FirstComponent },
{ path: 'second-component', component: SecondComponent },
{ path: '**', component: PageNotFoundComponent }, // Wildcard route for a 404 page
];
```
The last route with the `path` of `**` is a wildcard route.
The router selects this route if the requested URL doesn't match any of the paths earlier in the list and sends the user to the `PageNotFoundComponent`.
## Setting up redirects
To set up a redirect, configure a route with the `path` you want to redirect from, the `component` you want to redirect to, and a `pathMatch` value that tells the router how to match the URL.
```
const routes: Routes = [
{ path: 'first-component', component: FirstComponent },
{ path: 'second-component', component: SecondComponent },
{ path: '', redirectTo: '/first-component', pathMatch: 'full' }, // redirect to `first-component`
{ path: '**', component: PageNotFoundComponent }, // Wildcard route for a 404 page
];
```
In this example, the third route is a redirect so that the router defaults to the `first-component` route.
Notice that this redirect precedes the wildcard route.
Here, `path: ''` means to use the initial relative URL \(`''`\).
For more details on `pathMatch` see [Spotlight on `pathMatch`](guide/router-tutorial-toh#pathmatch).
## Nesting routes
As your application grows more complex, you might want to create routes that are relative to a component other than your root component.
These types of nested routes are called child routes.
This means you're adding a second `` to your app, because it is in addition to the `` in `AppComponent`.
In this example, there are two additional child components, `child-a`, and `child-b`.
Here, `FirstComponent` has its own `