角度http由id获取不起作用

时间:2017-04-06 17:50:02

标签: angular

我正在尝试按照英雄之旅教程。我有getHeroes()getHero(id),而不是getCustomers()getCustomer(id)。代码与教程中的代码相同。 getCustomers正在运行,但getCustomer()无效。我已经验证我的Web API控制器正在返回getCustomer()的JSON文件,就像getCustomers()一样。唯一的区别是我的id参数是string而不是number

我做错了什么?任何建议都将不胜感激。

customer.service.ts

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);
}

客户detail.component.ts

ngOnInit(): void {
    this.route.params
        .switchMap((params: Params) => this.customerService.getCustomer(params['id']))
        .subscribe((customer: Customer) => this.customer = customer);
}

0 个答案:

没有答案