TypeError:无法读取未定义的属性“back”

时间:2018-02-10 20:48:18

标签: angular

这是我的component.html

<button type="button"
    (click)="save()">Save</button>

这是我的component.ts

private heroService: HeroService;
private location: Location;
constructor(heroService: HeroService) {
    this.heroService = heroService;
}
ngOnInit() { }

@Input("hero") hero: Hero;

public save(): void {
    this.heroService.updateHero(this.hero)
        .subscribe(()=> this.goBack());
}
goBack(): void {
    this.location.back();
}

这是我的service.ts

private heroesUrl = 'api/heroes'; 
updateHero(hero: Hero): Observable<any> {
    return this.http.put(this.heroesUrl, hero, httpOptions).pipe(
        tap(_ => this.log(`updated hero id=${hero.id}`)),
        catchError(this.handleError<any>('updateHero'))
    );
}

当我在我的应用程序中单击“保存”时,我在location.back()时遇到以下错误叫做。

https://i.stack.imgur.com/S6F3V.png

1 个答案:

答案 0 :(得分:0)

使用HeroService进行组件构造函数中的位置

constructor(heroService: HeroService,
            location: Location) {
    this.heroService = heroService;
    this.location = location;
}