【发布时间】:2021-10-26 16:28:51
【问题描述】:
当 URL 地址不正确时,我正在尝试在 Angular 上创建一个页面的路由。
控制台出错
在我的 IDE 中没有错误消息。 我在控制台上收到的唯一错误消息是:
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)
以下是我的文件:
SingleAppareilComponentAppModule- 404 页面的 HTML 模板
-
SingleAppareilComponent的 HTML 模板 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 { }
它应该显示这个模板:
four-oh-four.component.html
<h2>Erreur 404</h2>
<p>La page que vous cherchez n'existe pas.</p>
但它会显示这个
single-appareil.component.hmtl
<h2>{{ name }}</h2>
<p>Statut : {{ status }}</p>
<a routerLink="/appareils">Retourner à la liste</a>
appareil-service.ts
export class AppareilService {
appareils = [
{
id: 1,
name: 'Machine à laver',
status: 'éteint'
},
{
id:2,
name: 'Frigo',
status: 'allumé'
},
{
id:3,
name: 'Ordinateur',
status: 'éteint'
}
];
getApparreilById(id: number) {
const appareil = this.appareils.find(
(appareilObject) =>{
return appareilObject.id === id;
}
);
return appareil
}
switchOnAll() {
for (let appareil of this.appareils) {
appareil.status = 'allumé'
}
}
switchOffAll() {
for (let appareil of this.appareils) {
appareil.status = 'éteint'
}
}
switchOnOne(index: number) {
this.appareils[index].status = 'allumé';
}
switchOffOne(index: number) {
this.appareils[index].status = 'éteint';
}
【问题讨论】:
-
可能
this.appareilService.getApparreilById(+id)返回undefined。打开浏览器的开发工具,进入调试器,在ngOnInit设置断点,调试服务方法this.appareilService.getApparreilById -
将有助于查看您的
AppareilService。显然,当通过appareils/:id路由到 SingleAppareilComponent 时,它会为给定的id参数返回undefined。
标签: javascript angular typescript routes