我正在尝试按照英雄之旅教程。我有getHeroes()
和getHero(id)
,而不是getCustomers()
和getCustomer(id)
。代码与教程中的代码相同。 getCustomers
正在运行,但getCustomer()
无效。我已经验证我的Web API控制器正在返回getCustomer()
的JSON文件,就像getCustomers()
一样。唯一的区别是我的id
参数是string
而不是number
。
我做错了什么?任何建议都将不胜感激。
getCustomers(): Observable<Customer[]> {
return this.http.get(this.customerUrl)
.map(this.extractData)
.catch(this.handleError);
}
getCustomer(id: string): Observable<Customer> {
const url = `${this.customerUrl}/${id}`;
return this.http.get(url)
.map(this.extractData)
.catch(this.handleError);
}
ngOnInit(): void {
this.route.params
.switchMap((params: Params) => this.customerService.getCustomer(params['id']))
.subscribe((customer: Customer) => this.customer = customer);
}