“样本的捕获错误”;https://angular.io/tutorial/toh-pt6" 它';它不工作了

“样本的捕获错误”;https://angular.io/tutorial/toh-pt6" 它';它不工作了,angular,typescript,phpstorm,angular7,Angular,Typescript,Phpstorm,Angular7,如果复制Angular的docs页面上公开的hero.service.ts类,则调用catchError(this.handleError)时会看到一个问题 遵循完整的typescript类: import { Injectable } from '@angular/core'; import { HttpClient, HttpHeaders } from '@angular/common/http'; import { Observable, of } from 'rxjs'; impor

如果复制Angular的docs页面上公开的hero.service.ts类,则调用
catchError(this.handleError)时会看到一个问题

遵循完整的typescript类:

import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';

import { Observable, of } from 'rxjs';
import { catchError, map, tap } from 'rxjs/operators';

import { Hero } from './hero';
import { MessageService } from './message.service';

const httpOptions = {
  headers: new HttpHeaders({ 'Content-Type': 'application/json' })
};

@Injectable({ providedIn: 'root' })
export class HeroService {

  private heroesUrl = 'api/heroes';  // URL to web api

  constructor(
    private http: HttpClient,
    private messageService: MessageService) { }

    /** PUT: update the hero on the server */
      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'))
        );
      }
    /**
       * Handle Http operation that failed.
       * Let the app continue.
       * @param operation - name of the operation that failed
       * @param result - optional value to return as the observable result
       */
      private handleError<T> (operation = 'operation', result?: T) {
        return (error: any): Observable<T> => {

          // TODO: send the error to remote logging infrastructure
          console.error(error); // log to console instead

          // TODO: better job of transforming error for user consumption
          this.log(`${operation} failed: ${error.message}`);

          // Let the app keep running by returning an empty result.
          return of(result as T);
        };
      }

      /** Log a HeroService message with the MessageService */
      private log(message: string) {
        this.messageService.add(`HeroService: ${message}`);
      }
    }
从'@angular/core'导入{Injectable};
从'@angular/common/http'导入{HttpClient,HttpHeaders};
从'rxjs'导入{可观察的};
从“rxjs/operators”导入{catchError,map,tap};
从“/Hero”导入{Hero};
从“./message.service”导入{MessageService};
常量httpOptions={
标题:新的HttpHeaders({'Content-Type':'application/json'})
};
@可注射({providedIn:'root'})
导出类服务{
private heroesUrl='api/heroes';//指向web api的URL
建造师(
私有http:HttpClient,
私有消息服务:消息服务){}
/**放置:更新服务器上的英雄*/
更新英雄(英雄:英雄):可见{
返回this.http.put(this.heroesUrl,hero,httpOptions).pipe(
轻触(=>this.log(`updated hero id=${hero.id}`)),
catchError(this.handleError('updateHero'))
);
}
/**
*处理失败的Http操作。
*让应用程序继续。
*@param operation-失败的操作的名称
*@param result-作为可观察结果返回的可选值
*/
私有句柄错误(操作='operation',结果?:T){
返回(错误:任意):可观察=>{
//TODO:将错误发送到远程日志记录基础结构
console.error(error);//改为登录到控制台
//TODO:更好地转换错误以供用户使用
此.log(`operation}失败:${error.message}`);
//通过返回空结果让应用程序继续运行。
返回(结果为T);
};
}
/**使用MessageService记录HeroService消息*/
私有日志(消息:字符串){
this.messageService.add(`HeroService:${message}`);
}
}
请遵循以下错误:

Argument type (error:any)=>Observable<Cart> is not assignable to parameter type (err:any, caught:Observable<T>)=> never
参数类型(error:any)=>Observable不可分配给参数类型(err:any,catch:Observable)=>never

我在这里找到了答案,我真的不明白为什么您也看不到错误。但它发生在这里

我更改了handleError的声明、参数的声明和返回一个never类型。我还必须在日志记录期间更改error对象的访问字段

遵循最终代码:

/** POST: add a new hero to the server */
  save(): Observable<Hero> {

    var o : Observable<Hero>=this.http.post<Hero>(
        "http://localhost/FoodMail/master/src/webservice.php?q=SalEBrasa", c, httpOptions)
        .pipe(
            tap((hero: Hero) => {this.log(`added hero w/ id=${hero.id}`)}),
            catchError<Hero>(this.handleError2.bind(this))
        );
    o.subscribe();
    return o;
  }
  private  handleError2<T>(error: any, caught: Observable<T>) : never{


    // TODO: send the error to remote logging infrastructure
    console.error(error.message); // log to console instead

    // TODO: better job of transforming error for user consumption
    this.log(`${error.statusText} failed: ${error.message}`);

    // Let the app keep running by returning an empty result.
    //return of(err as T);
    throw  error;

  }
/**帖子:向服务器添加新英雄*/
save():可观察{
var o:Observable=this.http.post(
"http://localhost/FoodMail/master/src/webservice.php?q=SalEBrasa“,c,httpOptions)
.烟斗(
点击((hero:hero)=>{this.log(`added hero w/id=${hero.id})}),
catchError(this.handleError2.bind(this))
);
o、 订阅();
返回o;
}
private handleError2(错误:任意,捕获:可观察):从不{
//TODO:将错误发送到远程日志记录基础结构
console.error(error.message);//改为登录到控制台
//TODO:更好地转换错误以供用户使用
此.log(`error.statusText}失败:${error.message}`);
//通过返回空结果让应用程序继续运行。
//返回(错误为T);
投掷误差;
}

如果Angular文档中有错误,你应该通过GitHub向他们报告。你有问题吗?我的问题是如何纠正错误,这是一个记录的语法类型脚本错误,而不是Angular7上的错误。这是我将问题放在这里的原因,而不是放在GitHub中。我几乎在这里找到了解决方案,如果它有效,我会的把答案放在这里,这不是语法错误,这是类型错误。你可以通过使用适当的类型来纠正它。但是,如果官方文档中有错误的例子,那就是文档的问题。使用什么样的适当类型?这是个问题,你能分享吗?它会在错误消息中告诉你。它会告诉你什么是handleError当前返回,以及catchError所期望的内容。