I am working on implementing a route to a page in Angular for handling incorrect URL addresses.
Error Received in Console
While there is no error message in my IDE, the console displays the following error:
ERROR TypeError: Cannot read property 'name' of undefined
at SingleAppareilComponent.ngOnInit (single-appareil.component.ts:19)
at callHook (core.js:2526)
at callHooks (core.js:2495)
at executeInitAndCheckHooks (core.js:2446)
at refreshView (core.js:9480)
at refreshEmbeddedViews (core.js:10590)
at refreshView (core.js:9489)
at refreshComponent (core.js:10636)
at refreshChildComponents (core.js:9261)
at refreshView (core.js:9515)
Here are the files involved:
SingleAppareilComponent
AppModule
- HTML template for 404 page
- HTML template for
SingleAppareilComponent
AppareilService
single.appareil.component.ts
import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { AppareilService } from 'services/appareil.service';
@Component({
selector: 'app-single-appareil',
templateUrl: './single-appareil.component.html',
styleUrls: ['./single-appareil.component.scss']
})
export class SingleAppareilComponent implements OnInit {
name: string = 'Appareil';
status: string = 'Statut';
constructor(private appareilService: AppareilService,
private route: ActivatedRoute) { }
ngOnInit(): void {
const id = this.route.snapshot.params['id'];
this.name = this.appareilService.getApparreilById(+id).name;
this.status = this.appareilService.getApparreilById(+id).status;
}
}
app.module.ts
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { AppareilComponent } from './appareil/appareil.component';
import { FormsModule } from '@angular/forms';
import { AppareilService } from 'services/appareil.service';
import { AuthComponent } from './auth/auth.component';
import { AppareilViewComponent } from './appareil-view/appareil-view.component';
import { RouterModule, Routes } from '@angular/router';
import { AuthService } from 'services/auth.service';
import { SingleAppareilComponent } from './single-appareil/single-
appareil.component';
import { FourOhFourComponent } from './four-oh-four/four-oh-four.component';
import { HashLocationStrategy, LocationStrategy } from '@angular/common';
const appRoutes: Routes = [
{ path: 'appareils', component: AppareilViewComponent },
{ path: 'appareils/:id', component: SingleAppareilComponent },
{ path: 'auth', component: AuthComponent },
{ path: '', component: AppareilViewComponent },
{ path: 'not-found', component: FourOhFourComponent },
{ path:'**', redirectTo: '/notfound' }
]
@NgModule({
declarations: [
AppComponent,
AppareilComponent,
AuthComponent,
AppareilViewComponent,
SingleAppareilComponent,
FourOhFourComponent
],
imports: [
BrowserModule,
AppRoutingModule,
FormsModule,
RouterModule.forRoot(appRoutes)
],
providers: [
AppareilService,
AuthService,
{provide: LocationStrategy, useClass: HashLocationStrategy}
],
bootstrap: [AppComponent]
})
export class AppModule { }
The expected template to be displayed:
four-oh-four.component.html
<h2>Error 404</h2>
<p>The page you are looking for does not exist.</p>
However, the current template being displayed is:
single-appareil.component.hmtl
<h2>{{ name }}</h2>
<p>Status: {{ status }}</p>
<a routerLink="/appareils">Return to list</a>
appareil-service.ts
export class AppareilService {
appareils = [
{
id: 1,
name: 'Washing Machine',
status: 'off'
},
{
id:2,
name: 'Fridge',
status: 'on'
},
{
id:3,
name: 'Computer',
status: 'off'
}
];
getApparreilById(id: number) {
const appareil = this.appareils.find(
(appareilObject) =>{
return appareilObject.id === id;
}
);
return appareil
}
switchOnAll() {
for (let appareil of this.appareils) {
appareil.status = 'on'
}
}
switchOffAll() {
for (let appareil of this.appareils) {
appareil.status = 'off'
}
}
switchOnOne(index: number) {
this.appareils[index].status = 'on';
}
switchOffOne(index: number) {
this.appareils[index].status = 'off';
}