Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Handling Non-Existent Routes in Angular | Routing and Navigation in Angular
Introduction to Angular
course content

Kursinnhold

Introduction to Angular

Introduction to Angular

1. Angular Fundamentals
2. Components and Templates
3. Mastering Angular Directives and Pipes
4. Services and Dependency Injection in Angular
5. Standalone Components & Modules
6. Routing and Navigation in Angular

book
Handling Non-Existent Routes in Angular

Sometimes users might type in a wrong URL manually or click on an outdated link. In such cases, it's important to show a clear "Page Not Found" message instead of a blank screen or a technical error. In this module, we'll create a 404 page component, configure our routes to display it for all unknown paths, and add some polished styling.

Creating the NotFoundComponent

We'll create a new Angular component called NotFoundComponent to serve as our 404 page. It will display a headline, a short error message, and a button to redirect the user back to the homepage.

Run the following command to generate the component:

This will automatically create a not-found folder with four files. You're already familiar with these files, and you can go ahead and delete the test file if you're not using it.

Building the Component

Let's add the HTML to show a "404" title, a simple message, and a "Go to Home" button:

html

not-found-conponent

css

not-found-conponent

copy
12345
<div class="not-found"> <h2>404 - Page Not Found</h2> <p>Oops! Looks like this page doesn’t exist 😢</p> <button (click)="goToHome()">Go to Home</button> </div>

It's a simple layout, with one special part — the goToHome() method. When the button is clicked, the user is redirected to the homepage.

Note
Note

We've also added some CSS to make the 404 page visually appealing — centered text, a noticeable heading, and a clean, bright button.

Adding the Navigation Logic

Now let's implement the method to navigate the user back to the home page.

ts

not-found-conponent

copy
123456789101112
@Component({ selector: 'app-not-found', templateUrl: './not-found.component.html', styleUrls: ['./not-found.component.css'] }) export class NotFoundComponent { constructor(private router: Router) {} goToHome() { this.router.navigate(['/']); } }

Here, we inject Angular's Router to manually navigate between routes. In the goToHome() method, we use navigate(['/']) to send the user to the root route.

Handling All Unknown Routes

Now we'll update the app's routing configuration to show the NotFoundComponent for any undefined paths.

In your app.routes.ts file, add the following route at the very end:

ts

routes

copy
12345678910
import { Routes } from '@angular/router'; import { TaskListComponent } from './components/task-list/task-list.component'; import { BibaComponent } from './components/biba/biba.component'; import { NotFoundComponent } from './not-found/not-found.component'; export const routes: Routes = [ { path: '', component: TaskListComponent }, { path: 'task/:id', component: BibaComponent }, { path: '**', component: NotFoundComponent } ];

The ** path is a wildcard that matches any route that doesn't match earlier ones. For example, navigating to /wrong-url will display the 404 page.

Note
Note

Always place the wildcard route last to avoid catching valid routes by accident.

Now your app gracefully handles navigation errors: instead of crashing or showing a blank screen, users see a friendly 404 message with a clear way back to the homepage.

Your app is now fully functional and user-friendly! 🎉 If you'd like to download the complete version of this project, just click the button below.

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 6. Kapittel 5

Spør AI

expand
ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

course content

Kursinnhold

Introduction to Angular

Introduction to Angular

1. Angular Fundamentals
2. Components and Templates
3. Mastering Angular Directives and Pipes
4. Services and Dependency Injection in Angular
5. Standalone Components & Modules
6. Routing and Navigation in Angular

book
Handling Non-Existent Routes in Angular

Sometimes users might type in a wrong URL manually or click on an outdated link. In such cases, it's important to show a clear "Page Not Found" message instead of a blank screen or a technical error. In this module, we'll create a 404 page component, configure our routes to display it for all unknown paths, and add some polished styling.

Creating the NotFoundComponent

We'll create a new Angular component called NotFoundComponent to serve as our 404 page. It will display a headline, a short error message, and a button to redirect the user back to the homepage.

Run the following command to generate the component:

This will automatically create a not-found folder with four files. You're already familiar with these files, and you can go ahead and delete the test file if you're not using it.

Building the Component

Let's add the HTML to show a "404" title, a simple message, and a "Go to Home" button:

html

not-found-conponent

css

not-found-conponent

copy
12345
<div class="not-found"> <h2>404 - Page Not Found</h2> <p>Oops! Looks like this page doesn’t exist 😢</p> <button (click)="goToHome()">Go to Home</button> </div>

It's a simple layout, with one special part — the goToHome() method. When the button is clicked, the user is redirected to the homepage.

Note
Note

We've also added some CSS to make the 404 page visually appealing — centered text, a noticeable heading, and a clean, bright button.

Adding the Navigation Logic

Now let's implement the method to navigate the user back to the home page.

ts

not-found-conponent

copy
123456789101112
@Component({ selector: 'app-not-found', templateUrl: './not-found.component.html', styleUrls: ['./not-found.component.css'] }) export class NotFoundComponent { constructor(private router: Router) {} goToHome() { this.router.navigate(['/']); } }

Here, we inject Angular's Router to manually navigate between routes. In the goToHome() method, we use navigate(['/']) to send the user to the root route.

Handling All Unknown Routes

Now we'll update the app's routing configuration to show the NotFoundComponent for any undefined paths.

In your app.routes.ts file, add the following route at the very end:

ts

routes

copy
12345678910
import { Routes } from '@angular/router'; import { TaskListComponent } from './components/task-list/task-list.component'; import { BibaComponent } from './components/biba/biba.component'; import { NotFoundComponent } from './not-found/not-found.component'; export const routes: Routes = [ { path: '', component: TaskListComponent }, { path: 'task/:id', component: BibaComponent }, { path: '**', component: NotFoundComponent } ];

The ** path is a wildcard that matches any route that doesn't match earlier ones. For example, navigating to /wrong-url will display the 404 page.

Note
Note

Always place the wildcard route last to avoid catching valid routes by accident.

Now your app gracefully handles navigation errors: instead of crashing or showing a blank screen, users see a friendly 404 message with a clear way back to the homepage.

Your app is now fully functional and user-friendly! 🎉 If you'd like to download the complete version of this project, just click the button below.

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 6. Kapittel 5
Vi beklager at noe gikk galt. Hva skjedde?
some-alt